adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

LiteLLM doesn't support structured output correctly.

Open jacobwisniewski opened this issue 1 year ago • 9 comments

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

jacobwisniewski avatar Jul 15 '25 01:07 jacobwisniewski

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.

BedirT avatar Jul 17 '25 19:07 BedirT

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

jacobwisniewski avatar Jul 30 '25 03:07 jacobwisniewski

I have the same issue!

ngothetai avatar Sep 29 '25 07:09 ngothetai

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!

surajksharma07 avatar Oct 10 '25 13:10 surajksharma07

Marking this issue as closed due to inactivity.

surajksharma07 avatar Nov 03 '25 09:11 surajksharma07

We are looking into it.

hangfei avatar Nov 03 '25 21:11 hangfei

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.

csp-andreasglashauser avatar Nov 04 '25 11:11 csp-andreasglashauser

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.

hangfei avatar Nov 04 '25 18:11 hangfei

Thank you for raising this! We added this in the next release, please reopen if it does not help your issue

GWeale avatar Nov 10 '25 05:11 GWeale