fix(google-genai): prevent TypeError when handling Pydantic model class #3931
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.
Description
This PR fixes a bug in the
opentelemetry-instrumentation-google-genaipackage where passing an uninstantiated Pydantic model class (instead of an instance) to dict conversion functions would cause aTypeError:The issue occurred in three functions that attempt to convert objects to dictionaries:
_to_dict()ingenerate_content.py_to_otel_value()intool_call_wrapper.py_flatten_dict()indict_util.pyThese functions check
hasattr(value, "model_dump")which returnsTruefor both Pydantic classes and instances, butmodel_dump()is an instance method that cannot be called on the class itself.Solution:
isinstance(value, type)check to distinguish between classes and instances before callingmodel_dump()model_json_schema()(a classmethod) to get the schema representationFixes #3596
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Added unit tests to verify the fix:
test_flatten_with_pydantic_class_not_instance in
tests/utils/test_dict_util.pymodel_dump()errormodel_json_schema()test_handles_pydantic_class_not_instance in
tests/utils/test_tool_call_wrapper.pyDoes This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.