fix(py): surface action context on span metadata so Dev UI can render it#5364
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces action context surfacing in telemetry spans, allowing the Dev UI to display context details like authentication and headers. It includes new helper functions to recursively sanitize context data by removing non-JSON-serializable values. Feedback focused on optimizing these sanitization helpers by unifying redundant logic and improving performance through direct type checking or initial serialization attempts. Additionally, it was recommended to enhance error handling to account for potential recursion errors, ensuring that telemetry processing remains robust and does not impact the main execution flow.
ssbushi
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Discovered a bug while testing the
basic-flowssample.Run any Python flow with a context — say
withContextfrompy/samples/basic-flows— and pass{"auth": {"username": "pavel", "email": "[pavel@example.com](mailto:pavel@example.com)"}, "role": "admin"}. The flow itself sees the context correctly (ctx.contextworks inside the function), but the Dev UI trace inspector's Context panel is empty.Root cause
The trace consumer (Dev UI, downstream tools) reads action context off the span attribute
genkit:metadata:context. The JS SDK writes this injs/core/src/action.ts:Python's
Action._run_with_telemetrywas constructingSpanMetadatafromself._span_metadataonly —ctx.contextwas never serialized onto the span. So the attribute simply did not exist on Python-emitted spans, and the Dev UI had nothing to render.Fix
In
py/packages/genkit/src/genkit/_core/_action.py, copy the relevant span-metadata entries into a local dict, and ifctx.contextis non-empty, add acontextkey whose value is the JSON-encoded context. The existing tracing layer (_tracing.run_in_new_span) already prefixes metadata keys withgenkit:metadata:and stores them as strings, so this lands on the wire asgenkit:metadata:context— exactly the attribute the Dev UI already knows how to read.JSON-encoded over
str(...)so the Dev UI can parse it back into a real object for the tree view (matches JS). Falls back tostr(...)if the context happens to contain something non-JSON-serializable, rather than failing the whole span.Testing
Verified manually in Dev UI.