Fix response_format parameter to support Pydantic BaseModel classes (Issue #3959) #3960
+156
−5
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.
Fix response_format to support Pydantic BaseModel classes (#3959)
Summary
This PR restores backwards compatibility for the
response_formatparameter in the LLM class, which was broken in PR #3793. Users can now pass Pydantic BaseModel classes directly toresponse_formatwhen initializing an LLM instance, and the framework will automatically convert them to the json_schema format required by LiteLLM.Changes:
_prepare_completion_paramsto detect Pydantic BaseModel classes inresponse_formatand convert them to{"type": "json_schema", "json_schema": {...}}format_handle_non_streaming_responseto parse JSON responses back into Pydantic models whenresponse_formatis a BaseModel classresponse_modelparameter (passed tocall()) takes precedence overresponse_format(set at init)@pytest.mark.vcrdecorator fromanthropic_llmfixture that was causing test collection errors)Review & Testing Checklist for Human
response_formatto verify the fix works in production (the tests only use mocks)response_format(e.g.,{"type": "json_object"}) still works correctlyresponse_formatmay need additional workTest Plan
Notes
model_json_schema()which is Pydantic v2 syntax (correct for this codebase)@pytest.mark.vcr) was necessary to allow test collection to succeedLink to Devin run: https://app.devin.ai/sessions/3672f697c80b4ae0bfea447d19ee34a6
Requested by: João ([email protected])