fix(litellm): Pass response_format to LiteLLM completion for structured outputs #5881
+582
−28
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
Summary of changes: Fix LiteLLM model to properly pass
response_formatto the underlying LiteLLM library when structured outputs are enabled.Related issues: N/A (or link to any related issues)
Motivation and context:
When using
LiteLLMmodel withoutput_schema,structured_outputs=True, andsupports_native_structured_outputs=True, the agent was still falling back to JSON mode via prompt injection. This was because theresponse_formatparameter was never passed tolitellm.completion().Environment or dependencies: No changes required
Type of change
Changes Made
Added explicit structured output flags with documentation:
Falseby default (safe for providers like Anthropic, Ollama)Added
_get_response_format_param()helper method:NoneforNoneinputAdded
_add_additional_properties_false()helper method:additionalProperties: falseto all object schemasUpdated all invoke methods (
invoke,invoke_stream,ainvoke,ainvoke_stream):response_formattocompletion_kwargsresponse_formatwhensupports_native_structured_outputs=TrueAdded tests for structured output functionality
Important Design Decision
The
supports_native_structured_outputsflag is kept asFalseby default because:response_formatparameter (causes 400 error)The
response_formatparameter is only passed to the API whensupports_native_structured_outputs=True.This ensures providers like Anthropic continue to work (falling back to JSON mode via prompt).
Users enable it explicitly when they know their model supports it:
Checklist
./scripts/format.shand./scripts/validate.shTesting
Tested with:
gpt-4o)sap/gpt-4o-mini)Before fix:
After fix (with
supports_native_structured_outputs=True):