Skip to content

fix(py): surface action context on span metadata so Dev UI can render it#5364

Merged
huangjeff5 merged 4 commits into
mainfrom
jh-fix-context
May 21, 2026
Merged

fix(py): surface action context on span metadata so Dev UI can render it#5364
huangjeff5 merged 4 commits into
mainfrom
jh-fix-context

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented May 20, 2026

Copy link
Copy Markdown
Contributor

Context

Discovered a bug while testing the basic-flows sample.

Run any Python flow with a context — say withContext from py/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.context works 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 in js/core/src/action.ts:

if (options?.context) {
  setCustomMetadataAttributes({
    context: JSON.stringify(options.context),
  });
}

Python's Action._run_with_telemetry was constructing SpanMetadata from self._span_metadata only — ctx.context was 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 if ctx.context is non-empty, add a context key whose value is the JSON-encoded context. The existing tracing layer (_tracing.run_in_new_span) already prefixes metadata keys with genkit:metadata: and stores them as strings, so this lands on the wire as genkit: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 to str(...) if the context happens to contain something non-JSON-serializable, rather than failing the whole span.

Testing

Verified manually in Dev UI.

bug1-context-in-devui

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread py/packages/genkit/src/genkit/_core/_action.py Outdated
Comment thread py/packages/genkit/src/genkit/_core/_action.py Outdated
@huangjeff5 huangjeff5 requested a review from a team May 20, 2026 21:18
@huangjeff5 huangjeff5 merged commit b966f83 into main May 21, 2026
19 checks passed
@huangjeff5 huangjeff5 deleted the jh-fix-context branch May 21, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants