Repository navigation
fix(py/otel): serialize Pydantic tool call arguments as valid JSON - #7098
Closed
huangjeff5 wants to merge 2 commits into
Closed
huangjeff5 wants to merge 2 commits into
huangjeff5 wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a helper function _encode_json_value to serialize Pydantic models to JSON before setting span attributes or recording tool content, along with a unit test to verify this behavior. However, the current implementation only handles top-level BaseModel instances and fails to serialize nested Pydantic models inside lists or dictionaries. It is recommended to use a custom default encoder function within json.dumps to dynamically handle nested Pydantic models.
Contributor
Author
|
Closing: migrated to standalone repository genkit-ai/genkit-python (PR #39): genkit-ai/genkit-python#39 |
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.
Problem
When instrumenting Genkit tool executions with
GenAiInstrumentation(withemit_tool_spans=Trueor content capturing modes enabled), toolinputis typically a PydanticBaseModelinstance._set_json_attributeand_record_tool_contentpreviously serialized inputs directly viajson.dumps(value, default=str). Because Pydantic models fall through tostr(model),gen_ai.tool.call.argumentswas written as a stringified Python repr (e.g."OrderInput(dish='pad thai', quantity=2)") rather than valid JSON ({"dish": "pad thai", "quantity": 2}), violating OpenTelemetry GenAI semantic conventions.Mechanism
Introduced
_encode_json_value(value: object) -> str:valueis an instance ofpydantic.BaseModel, callsvalue.model_dump(mode='json')before JSON serialization._set_json_attributeand_record_tool_contentfor both span attributes and OTel log events.Verification
test_tool_arguments_pydantic_serialized_as_valid_jsoninpackages/genkit-otel/tests/genai_instrumentation_test.pyverifying that Pydantic inputs are serialized to valid JSON readable viajson.loads()on both tool spans and operation details log events.pytest packages/genkit-otel(77 passed).ruff check,ruff format, andpyrightclean.