Fix #985: Handle PydanticInvalidForJsonSchema for unserializable BaseModel - #1032
Open
hulincup wants to merge 3 commits into
Open
Fix #985: Handle PydanticInvalidForJsonSchema for unserializable BaseModel#1032hulincup wants to merge 3 commits into
hulincup wants to merge 3 commits into
Conversation
… schemas When a BaseModel contains fields that cannot be converted to JSON schema (e.g., Callable fields), connections now gracefully fall back to prompt engineering instead of raising PydanticInvalidForJsonSchema. Changes: - OpenAI: Add exception handling in _native_response_format() - Azure: Add exception handling in _native_response_format() - Anthropic: Add exception handling in _native_output_config() - Add warning logs when falling back to prompt engineering - Update Anthropic docstring to document the fallback behavior - Add comprehensive test coverage for all three connections Fixes apache#985
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.
Description
Fixes #985
When a BaseModel contains fields that cannot be serialized to JSON schema (e.g.,
Callablefields), theto_strict_json_schema()function raisesPydanticInvalidForJsonSchema. Currently, this exception propagates up and breaks the chat call.The issue description notes that
RowTypeInfoschemas gracefully fall back to prompt engineering when the connection can't translate them natively, butBaseModelschemas that can't be serialized raise an exception instead. This inconsistency should be fixed.Changes
This PR catches
PydanticInvalidForJsonSchemain the three connection types that useto_strict_json_schema()ortransform_schema():OpenAI (
openai_chat_model.py):_native_response_format()None)loggerimportAzure OpenAI (
azure_openai_chat_model.py):_native_response_format()None)Anthropic (
anthropic_chat_model.py):_native_output_config()None)Testing
Added comprehensive test file
test_unserializable_output_schema.pywith three test cases:test_openai_handles_unserializable_schema(): Verifies OpenAI falls back to prompt when schema cannot be serializedtest_azure_handles_unserializable_schema(): Verifies Azure falls back to prompt when schema cannot be serializedtest_anthropic_handles_unserializable_schema(): Verifies Anthropic falls back to prompt when schema cannot be serializedEach test uses a
BadModelwith aCallablefield that cannot be converted to JSON schema, and verifies that:Verification
The fix ensures that:
BaseModelschemas gracefullyRowTypeInfofallbackRelated Issues