<@ULVA73B9P> TLDR Can I emit the payload fields as...
# ask-marvin
r
@Marvin TLDR Can I emit the payload fields as objects, from events? E.g. I see that when I use this:
Copy code
python
flow_emit_metrics.serve(
        name="emit-metrics",
        triggers=[ DeploymentEventTrigger(
            name="emit-metrics-on-event",
            threshold=1,
            posture=Posture.Reactive,
            description="Emit metrics on event",
            enabled=True,
            within=1,
            expect=[insert_metrics_event_name],
            parameters={
                "metrics_config": "{{event.payload.metrics_config}} | tojson",
                "labels": "{{event.payload.labels}} | tojson",
            }
        ) ],
    )
but then I get this error:
Copy code
Action failed: 'Validation error occurred for \'run-deployment\' - Error creating flow run: Validation failed for field \'metrics_config\'. Failure reason: "{\'number_of_rows\': [10, \'Number of rows inserted\', \'counter\']}" is not of type \'object\''
the signature of the flow is
Copy code
def flow_emit_metrics(metrics_config: dict, labels: dict):
I imagine jinja is converting the objects to strings, but I want to keep them as objects. Can I do that?
m
It looks like you're dealing with a common issue when using Jinja2 in Prefect to handle event payloads. The error arises because Jinja2 is converting objects to JSON strings, which aren't automatically converted back to objects when passing them to your flow. The Prefect documentation and resources suggest that you should indeed be able to handle this by ensuring that the inputs are parsed as JSON objects before they are used. When using Jinja2, strings need explicit conversion back to objects in your logic. 1. When you're using the
{{ event.payload.metrics_config }} | tojson
in your template, ensure that when using it in your Python logic, you parse it back into an object using the
json.loads
method from Python's standard library. Here's an example:
Copy code
python
   import json
   
   def flow_emit_metrics(metrics_config: dict, labels: dict):
       metrics_config = json.loads(metrics_config)
       labels = json.loads(labels)
       # Your flow logic here
This adjustment should help keep your payload fields as objects rather than plain strings. If humor could debug your code, it might say something like, "I think you ought to know I'm feeling very parsed now." For further details, you might want to look at Prefect's webhook guides or track activity through events. The unsuccessful GitHub query indicated no existing issues closely matching your precise problem, which suggests your approach might be slightly unique or less documented. Let me know if you need more help!
1
n
there's a pretty complicated way to get this to happen today that we'd like to simplify, but casting back to an object inside is always a safe bet 👍 here's the sort of complex way though if you want to try it in the interim
Copy code
trigger = DeploymentEventTrigger(  # type: ignore
    expect={"prefecthq/prefect*"},
    parameters={
        "event": {
            "__prefect_kind": "json",
            "value": {
                "__prefect_kind": "jinja",
                "template": "{{ event.payload | tojson }}",
            },
        }
    },
)