Skip to content

PydanticSerializationError when running LiteLlm based agent via adk web or api_server #6164

Closed

Description

@claud42

🔴 Required Information

Describe the Bug:
When developing agent by LiteLlm, the adk web or adk api_server command raise an exception due to LiteLlm having a client field in type of LiteLLMClient that can't be deserialized to be part of FastAPI's response.

Steps to Reproduce:

  1. Install adk
  2. Develop an agent by LiteLlm (see minimal reproducible code at the end)
  3. Run $ adk web --port 8000
  4. Open http://127.0.0.1:8000
  5. Error and stacktrace:
INFO:     127.0.0.1:57588 - "GET /dev/apps/my_agent/build_graph HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
<... skipped many lines> 
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'google.adk.models.lite_llm.LiteLLMClient'>

Expected Behavior:
No 500 error for the HTTP request.
No error message in adk web or adk api_server stdout/stderr.

Observed Behavior:
500 error in HTTP request.
Error message showed as in above.

Environment Details:

  • ADK Library Version (pip show google-adk): Version: 2.2.0
  • Desktop OS: macOS
  • Python Version (python -V): 3.14.4

Model Information:

  • Are you using LiteLLM: Yes
  • Which model is being used: google/gemma-4-26B-A4B-it

🟡 Optional Information

Additional Context:
Root cause:
LiteLlm.llm_client is a declared Pydantic field but its class LiteLLMClient has no JSON serializer. Any JSON serialization of a LiteLlm instance raises the error, e.g., when using FastAPI.

By contrast, Gemini exposes its api_client as a @cached_property, not a field, so it is never part of the serialized output.

A fix is obvious and trivial.

Minimal Reproduction Code:

model = LiteLlm(
    model="...",
    api_base="...",
)

root_agent = Agent(
    model=model,
    name='root_agent',
    description='A helpful assistant for user questions.',
    instruction='Answer user questions to the best of your knowledge',
)

How often has this issue occurred?:

  • Always (100%)

Activity

  1. added
    models[Component] This issue is related to model support
    on Jun 18, 2026
  2. added theissue type on Jun 18, 2026
  3. sanketpatil06 commented on Jun 19, 2026

    @sanketpatil06

    Hi @claud42 , Thanks for raising this issue and submitting PR.
    The PydanticSerializationError / TypeError occurs because LiteLlm includes an internal llm_client field (of type LiteLLMClient). When adk web or adk api_server serializes the agent graph using model_dump(), Pydantic attempts to serialize this live client instance, which is not JSON-serializable.

    Setting exclude=True on the llm_client field—as proposed in PR #6165—is the right approach, the PR is currently under review and will be merged upon approval.
    Workaround:
    In your agent entrypoint exclude the client field and force Pydantic to rebuild the schema:

    from google.adk.models.lite_llm import LiteLlm
    
    # Exclude the non-serializable client field and force Pydantic to rebuild the schema
    if hasattr(LiteLlm, "model_fields") and "llm_client" in LiteLlm.model_fields:
        LiteLlm.model_fields["llm_client"].exclude = True
        LiteLlm.model_rebuild(force=True)
    
  4. added a commit that references this issue on Jul 16, 2026
    ecbefd9
  5. added a commit that references this issue on Aug 11, 2026
    55c6f74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

models[Component] This issue is related to model support

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions