Skip to content

Custom Nodes in python #269

Description

@avasquezkudaw

Hi guys, I’m trying to create a custom_handler for custom nodes in Python, following examples written in Go (https://github.com/gorules/go-custom-nodes/blob/master/backend/main.go).

engine := zen.NewEngine(zen.EngineConfig{
		CustomNodeHandler: nodes.CustomNodeHandler,
	})

	decision, err := engine.CreateDecision(request.Content)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	result, err := decision.Evaluate(request.Context)
	if err != nil {
		var res gin.H
		if err := json.Unmarshal([]byte(err.Error()), &res); err == nil {
			c.JSON(http.StatusBadRequest, res)
			return
		}

		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"result":      result.Result,
		"trace":       result.Trace,
		"performance": result.Performance,
	})

However, I can't find documentation or methods to achieve this. I was able to create the custom nodes in the UI, but in the Python backend, I can’t get them to work, and when loading the graph with these custom nodes, I get the following error:

import zen, json
ze = zen.ZenEngine()
de = ze.create_decision(json.dumps({"contentType": "application/vnd.gorules.decision", "nodes": [{"type": "customNode", "content": {"kind": "mathAdd"}, "id": "f3cc69ea-324f-456b-9af0-2469e1461393", "name": "mathAdd1", "position": {"x": 550, "y": 205}}, {"type": "inputNode", "id": "4d57711f-a141-4a3a-8f52-42ede085fdac", "name": "request", "position": {"x": 265, "y": 105}}, {"type": "outputNode", "id": "0a14e0a6-bd71-4747-bcc5-64964fc39a87", "name": "response", "position": {"x": 885, "y": 215}}], "edges": [{"id": "6d2ee81e-0447-4cfb-b120-5181065488b8", "sourceId": "4d57711f-a141-4a3a-8f52-42ede085fdac", "type": "edge", "targetId": "f3cc69ea-324f-456b-9af0-2469e1461393"}, {"id": "1e13a8e6-0591-4197-90ee-7974fd2a41ca", "sourceId": "f3cc69ea-324f-456b-9af0-2469e1461393", "type": "edge", "targetId": "0a14e0a6-bd71-4747-bcc5-64964fc39a87"}]}))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Failed to serialize decision content

Caused by:
    missing field `config` at line 1 column 216


How can I approach this issue?

Thanks in advance. Best regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions