Skip to content

Commit c1e852f

Browse files
he-yufengboyangsvl
authored andcommitted
fix(cli): Serialize LiteLlm graph models safely
The graph serializer currently passes model fields through when their value is not a simple collection or nested agent. For LiteLlm that leaves the runtime LiteLLMClient object inside the serialized graph payload, so JSON encoding the graph fails. This patch serializes BaseLlm values using their model name string value.model to ensure JSON safety and match the web UI frontend's expected string schema. Merge #5956 closes #5949 Change-Id: Ie31f640f6d9bedd515fd85ad0d4de2f5ad1ef7d7
1 parent 928017d commit c1e852f

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/google/adk/cli/utils/graph_serialization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
logger = logging.getLogger("google_adk." + __name__)
2323

2424
from ...agents.base_agent import BaseAgent
25+
from ...models.base_llm import BaseLlm
2526
from ...tools.base_toolset import BaseToolset
2627

2728
# Node type mapping for cleaner lookup
@@ -227,6 +228,8 @@ def serialize_agent(agent: BaseAgent) -> dict[str, Any]:
227228
# Handle nested agents
228229
if isinstance(value, BaseAgent):
229230
agent_dict[field_name] = serialize_agent(value)
231+
elif isinstance(value, BaseLlm):
232+
agent_dict[field_name] = value.model
230233
# Handle simple types and collections
231234
elif isinstance(value, (str, int, float, bool, list, dict)):
232235
agent_dict[field_name] = value

tests/unittests/cli/utils/test_graph_serialization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
"""Tests for graph_serialization edge handling with routing maps."""
1616

17+
import json
18+
19+
from google.adk.agents import LlmAgent
1720
from google.adk.cli.utils.graph_serialization import serialize_agent
21+
from google.adk.models.lite_llm import LiteLlm
1822
from google.adk.tools.base_toolset import BaseToolset
1923
from google.adk.workflow import START
2024
from google.adk.workflow import Workflow
@@ -126,3 +130,15 @@ def __init__(self):
126130
assert len(result['tools']) == 1
127131
assert result['tools'][0]['name'] == 'MockToolset'
128132
assert result['tools'][0]['type'] == 'tool'
133+
134+
135+
def test_serialize_agent_with_litellm_model_is_json_safe() -> None:
136+
agent = LlmAgent(
137+
name='repro',
138+
model=LiteLlm(model='ollama_chat/llama3'),
139+
)
140+
141+
result = serialize_agent(agent)
142+
143+
assert result['model'] == 'ollama_chat/llama3'
144+
json.dumps(result)

0 commit comments

Comments
 (0)