Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions agent/agent_runtime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,14 @@ def sanitize_api_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]
filtered.append(msg)
messages = filtered

# 0. Coerce non-string tool message content to strings so the API never
# receives a dict/list in ``content`` (which OpenAI rejects with 400).
for msg in messages:
if msg.get("role") == "tool":
content = msg.get("content")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This blanket check also serializes valid OpenAI content-part lists. _tool_result_content_for_active_model intentionally returns such a list for multimodal results, so the final sanitizer would turn a vision payload into JSON text. Preserve valid lists and coerce only invalid tool-content shapes.

if not isinstance(content, str):
msg["content"] = json.dumps(content, ensure_ascii=False, default=str)

surviving_call_ids: set = set()
for msg in messages:
if msg.get("role") == "assistant":
Expand Down
11 changes: 11 additions & 0 deletions run_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3346,6 +3346,13 @@ def _tool_result_content_for_active_model(self, tool_name: str, result: Any) ->
the agent has a chance to recover.
"""
if not _is_multimodal_tool_result(result):
# Non-multimodal results must be string-safe for the API. MCP tools
# (and a few native helpers) can return Python dicts/lists that the
# OpenAI SDK serializes as nested objects, causing HTTP 400
# "invalid message content type: map[string]interface {}" because
# the API expects `content` to be a string or a content-parts list.
if not isinstance(result, str):
return json.dumps(result, ensure_ascii=False, default=str)
return result

content = result.get("content") or []
Expand Down Expand Up @@ -3658,6 +3665,10 @@ def _needs_kimi_tool_reasoning(self) -> bool:
or base_url_host_matches(self.base_url, "api.kimi.com")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Kimi/Ollama reasoning change is unrelated to the stated serialization fix. Please split it out: current main documents Kimi detection as host-driven specifically to avoid model-name routing (run_agent.py:5505-5516).

or base_url_host_matches(self.base_url, "moonshot.ai")
or base_url_host_matches(self.base_url, "moonshot.cn")
or (
"kimi" in (self.model or "").lower()
and self.provider == "ollama-cloud"
)
)

def _needs_deepseek_tool_reasoning(self) -> bool:
Expand Down
Loading