xiaozhi-esp32-server/main/xiaozhi-server/test
Brad Shannon 31cc365fcd fix(core): guard orphan tool messages in dialogue serialization
The intent REQLLM path (core/handle/intentHandler.py) records a tool result via
`Message(role="tool", content=text)` with no tool_call_id and no preceding
assistant tool_calls. `Dialogue.getMessages` serializes it as a function_response
anyway. When that history is later replayed to an OpenAI-compatible endpoint,
the endpoint derives `function_response.name` by matching the tool_call_id back
to a function_call; with no match the name is empty.

gemini-2.5-flash tolerated the empty name, but gemini-3.5-flash rejects the whole
request with HTTP 400 "GenerateContentRequest.contents[N].parts[0]
.function_response.name: Name cannot be empty", so every affected turn falls back
to "Sorry, the system is busy now." Only tool-using sessions and only the chat
turns that replay the accumulated tool results were affected.

Guard at the serialization boundary in `getMessages`: emit a role="tool" message
as a function_response only when a preceding assistant message carries a matching
tool_call id; otherwise demote it to a plain assistant context line so the turn
survives. Valid tool-call pairs — including empty-string ids that still match —
are unchanged. This defends against any orphan source, not just the intent path.

Adds self-contained regression tests (no conftest required).
2026-07-14 16:09:00 -04:00
..
test_orphan_tool_message.py fix(core): guard orphan tool messages in dialogue serialization 2026-07-14 16:09:00 -04:00