Skip to content

Commit ecbefd9

Browse files
GWealecopybara-github
authored andcommitted
fix: exclude LiteLlm llm_client from JSON serialization
The llm_client field holds a LiteLLMClient with no JSON representation, so serializing a LiteLlm agent (as adk web and api_server do when returning the agent graph) raised PydanticSerializationError and 500'd every request for LiteLlm-based agents. Exclude the client from serialization; it is runtime state, not data. Close #6164 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 949034831
1 parent 98fc036 commit ecbefd9

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/google/adk/models/lite_llm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2679,7 +2679,9 @@ class LiteLlm(BaseLlm):
26792679
llm_client: The LLM client to use for the model.
26802680
"""
26812681

2682-
llm_client: LiteLLMClient = Field(default_factory=LiteLLMClient)
2682+
# LiteLLMClient has no JSON serializer, so it is excluded from dumps to keep
2683+
# model_dump(mode="json") from raising.
2684+
llm_client: LiteLLMClient = Field(default_factory=LiteLLMClient, exclude=True)
26832685
"""The LLM client to use for the model."""
26842686

26852687
_additional_args: Dict[str, Any] = None

tests/unittests/models/test_litellm.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6335,3 +6335,14 @@ async def test_streaming_tool_call_brace_in_string_does_not_falsely_complete(
63356335
args_by_name = {p.function_call.name: p.function_call.args for p in parts}
63366336
assert args_by_name["my_func"] == json.loads(full_args_a)
63376337
assert args_by_name["other_func"] == json.loads(full_args_b)
6338+
6339+
6340+
def test_model_dump_json_excludes_llm_client():
6341+
lite_llm_model = LiteLlm(model="test_model")
6342+
6343+
dumped = lite_llm_model.model_dump(mode="json")
6344+
dumped_json = lite_llm_model.model_dump_json()
6345+
6346+
assert "llm_client" not in dumped
6347+
assert "llm_client" not in json.loads(dumped_json)
6348+
assert dumped["model"] == "test_model"

0 commit comments

Comments
 (0)