Skip to content

fix(py/otel): serialize Pydantic tool call arguments as valid JSON - #39

Merged
huangjeff5 merged 7 commits into
mainfrom
fix/py-otel-tool-call-arguments-json
Nov 11, 2026
Merged

huangjeff5 merged 7 commits into
mainfrom
fix/py-otel-tool-call-arguments-json

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

OpenTelemetry tool spans and events now serialize Pydantic arguments and results as JSON. Before, json.dumps(..., default=str) fell back to str(model), so gen_ai.tool.call.arguments held Pydantic's field string instead of parseable JSON.

# 1. Define a typed tool
@ai.tool()
async def place_order(order: OrderInput) -> OrderOutput: ...

# 2. Trace attribute: gen_ai.tool.call.arguments
# before
# => "dish='pad thai' quantity=2"   (JSON-quoted string)

# after
# => {"dish": "pad thai", "quantity": 2}

Changes

  • Pydantic-aware JSON encoding: New _encode_json_value dumps BaseModel instances, including ones nested in lists and dicts, via model_dump(mode='json', by_alias=True, exclude_none=True) so keys match what the model sent. Other non-JSON values still fall back to str().
  • Applied to every content attribute that used the old encoder: gen_ai.tool.call.arguments and gen_ai.tool.call.result on spans and on the operation-details log event, plus genkit:input / genkit:output when capture_action_io=True.
  • Plain strings are JSON-encoded on spans too: Span attributes used to pass a str through raw while the log event encoded it. Now both write ORD-555 as "ORD-555", so every JSON attribute round-trips through json.loads.

Breaking changes

None.

Cross-SDK

Python traces now carry gen_ai.tool.call.arguments / gen_ai.tool.call.result as standard JSON instead of a Python string form, so Dev UI and OTel backends can parse them. No action needed for JS, Go, or Dart owners.

@cabljac cabljac 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.

A few comments/questions

@huangjeff5
huangjeff5 force-pushed the fix/py-otel-tool-call-arguments-json branch from c8fa6b2 to 1fc198f Compare November 10, 2026 19:35
@huangjeff5
huangjeff5 enabled stacked auto-merge (squash) November 10, 2026 20:42
@huangjeff5
huangjeff5 force-pushed the fix/py-otel-tool-call-arguments-json branch from 1fc198f to fa849bd Compare November 11, 2026 06:23

@cabljac cabljac 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.

LGTM

genkit:input / genkit:output in core use model_dump_json(by_alias=True,
exclude_none=True). The tool call encoder dumped Python field names and
nulls, so the same object read differently on two attributes of one
trace and disagreed with the arguments the model actually sent.
_encode_json_value returned str values unchanged, so a tool taking
order_id: str wrote ORD-555 raw into gen_ai.tool.call.arguments and
json.loads failed downstream. Encode strings like every other value,
matching JS JSON.stringify. (BR-01)
@huangjeff5
huangjeff5 force-pushed the fix/py-otel-tool-call-arguments-json branch from fa849bd to 6e2f115 Compare November 11, 2026 14:37
stacked auto-merge was automatically disabled November 11, 2026 14:38

Replaced by a newer auto-merge request

@huangjeff5
huangjeff5 enabled stacked auto-merge (squash) November 11, 2026 14:38
@huangjeff5
huangjeff5 merged commit 6f700e8 into main Nov 11, 2026
26 of 44 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-otel-tool-call-arguments-json branch November 11, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants