LiteLLM doesn't support structured output correctly.
Describe the bug
When using LiteLLM with other models (even when using Gemini models), structured output doesn't seem to be correctly output. The output is "almost correct" but not valid. I'm currently just using the default implementation of structured output with ADK, but have seen other issues reference modifying the args of the LiteLLM instance, this isn't obvious nor documented though.
To Reproduce
Using implementation like this
structured_output_agent = Agent(
name="structured_output_agent",
model=LiteLlm(model="gemini-2.5-flash"),
description="Converts structured data into a user-friendly UI format.",
instruction="""
You are an assistant that takes structured input and generates a clear, concise UI response.
""",
output_schema=UIOutput,
disallow_transfer_to_parent=True,
disallow_transfer_to_peers=True,
)
The output isn't matching what is specified in UIOutput
e.g.
What is specified is like the following
class SelectorComponent(BaseModel):
component_type: Literal["selector"] = Field(
"selector", description="The type identifier for a selector component."
)
id: conlist(int, min_length=1) = Field(
..., description="List of unique ids. e.g. [12345, 67890]"
)
actions: conlist(str, min_length=1, max_length=2) = Field(
...,
description="List of possible user actions. Keep this text short.",
)
UIComponent = Union[
SelectorComponent,
]
class UIOutput(BaseModel):
components: List[UIComponent] = Field(
...,
description="A list of UI components to be enriched on the backend and then rendered in the application.",
)
But the output is missing key fields like actions
{
"components": [
{
"component_type": "selector",
"ids": [
3584831,
3970968,
8453870,
20884876,
3094994,
8932363,
19704840,
20941330,
3404925,
3404956
]
}
]
}
Expected behavior
Ensure all fields are returned including actions
{
"components": [
{
"component_type": "selector",
"ids": [
3584831,
3970968,
8453870,
20884876,
3094994,
8932363,
19704840,
20941330,
3404925,
3404956
],
"actions": ["Yes", "No"]
}
]
}
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
-
OS: [e.g. iOS]
-
Python version(python -V):
-
ADK version(pip show google-adk):
-
Mac
-
Python 3.13.5
-
Google ADK 1.5.0
Model Information: For example, which model is being used.
Using gemini-2.5-flash via LiteLLM being sourced from AI studio
Yeah +1 on this. It is completely ignoring the schema for me.
I debugged a bit - and the LLM output has no structure whatsoever. Funnily though when debugging i set up vscode debugger for properly investigating, and when i use the debugger it actually produces proper objects... otherwise its just an str output. I tried this quite a lot, im unsure of what causes this.
Yeah +1 on this. It is completely ignoring the schema for me.
I debugged a bit - and the LLM output has no structure whatsoever. Funnily though when debugging i set up vscode debugger for properly investigating, and when i use the debugger it actually produces proper objects... otherwise its just an str output. I tried this quite a lot, im unsure of what causes this.
I fixed this by specifying the response format in the LiteLlm config like the following
model=LiteLlm(
model="gemini-2.5-flash",
response_format={
"type": "json_object",
"response_schema": ui_model.model_json_schema()
}
),
Still a bit of a workaround and not really documented anywhere
I have the same issue!
Hi @jacobwisniewski
The issue is in lite_llm.py:667-670 where ADK passes the raw Pydantic class as response_format, but LiteLLM doesn't convert this properly for Gemini models. Your workaround is correct - explicitly converting to model_json_schema() works. We'll look more into it by converting Pydantic models to LiteLLM's expected format (json_schema with strict mode) in the ADK code. Thanks!
Marking this issue as closed due to inactivity.
We are looking into it.
I just found this issue when debugging something completely unrelated to adk-python.
We are looking into it.
@hangfei Do you already know if this issue is really related to adk-python? I use DSPy (which uses LiteLLM) with Gemini 2.5 Flash via OpenRouter.
Not always, but pretty often Structured Outputs are not available:
2025/11/04 08:32:47 WARNING dspy.adapters.json_adapter: Failed to use structured output format, falling back to JSON mode.
this issue is really related to adk-python? I use DSPy (which uses LiteLLM) with Gemini 2.5 Flash via OpenRouter. Not always, but pretty often Stru
Not sure. We are still debugging this. Thanks for sharing this.
Thank you for raising this! We added this in the next release, please reopen if it does not help your issue