Repository navigation
fix(llm): preserve slash-containing message IDs in tool grouping - #8815
remote-controlled-man wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| parent_id, separator, call_index = item.id.rpartition("/fnc_") | ||
| group_id = item.group_id or ( | ||
| parent_id | ||
| if separator and call_index.isdecimal() | ||
| else item.id.split("/")[0] # legacy IDs without a generated suffix |
There was a problem hiding this comment.
🟡 Tool calls on user messages
When a standalone call’s /fnc_N suffix follows a full user message ID, group_tool_calls attaches it to that user message. OpenAI conversion then emits tool_calls on a user message instead of an assistant message.
Learn more
Applications can supply custom item IDs in a chat context, including through from_dict. The inference stream forwards that context to the provider formatter. Non-assistant items and tool groups share the same dictionary, so a parsed parent ID can resolve to a user message. OpenAI conversion keeps that message’s role and adds the tool calls to it, producing an invalid request.
Example: A user message with ID session/query is followed by a standalone call with ID session/query/fnc_0 and its result. Previously, conversion emitted a user message, a separate assistant tool-call message, and the result. It now puts the tool call directly on the user message.
Recommended fix: Give non-assistant items a separate key namespace from assistant messages and tool calls in the same ordered mapping. Keep the full assistant ID, suffix parsing, and explicit group precedence within the assistant/tool namespace. Add session/query/fnc_0 to the existing test for standalone calls next to session/query.
Was this helpful? React with 👍 or 👎 to provide feedback.
1AbdullahButt
left a comment
There was a problem hiding this comment.
Requesting changes for one narrow case. Assistant messages are now keyed by their full ID, but the legacy fallback still cuts call IDs at the first slash, so a slash-containing reply no longer matches its own calls when they lack /fnc_N.
In Docker (python 3.12, livekit-agents installed with --no-sources), tests/test_chat_ctx.py gave 106 passed on the head, and 8 of the new cases fail on the base. Then I ran to_provider_format("openai") on both:
- A. A reply
conversation/reply/1with callconversation/reply/1/call_1. The base gives one assistant message with text andtool_calls. The head splits it into a text message and a separate tool-call message. - B. Replies
conversationandconversation/reply/2, with the callconversation/reply/2/call_1. The base raised the assertion. The head attaches the call to "Hello" and moves "Checking" after the tool result, which silently reorders the turn.
Suggestion: at livekit-agents/livekit/agents/llm/_provider_format/utils.py:170, fall back to the longest assistant-message ID that is a /-prefix of the call ID, and use the first segment only when none matches. I tried that locally: both cases come out right and all 106 tests pass. Please add A and B near tests/test_chat_ctx.py:375.
Chat histories with distinct assistant IDs such as
conversation/reply/1andconversation/reply/2fail withAssertionError: only one message is allowed in a groupwhen converted for any supported LLM provider. The shared grouping helper treated everything before the first slash as the assistant ID.Keep the complete assistant message ID. For generated function calls, remove only the final
/fnc_Nsuffix when finding the parent; explicitgroup_idcontinues to take precedence. This preserves parallel call/result grouping when the assistant ID itself contains slashes.Validation:
make checkpassed: repository formatting (1,240 files), Ruff and strict mypy (796 source files).uv run --no-sync pytest tests/test_chat_ctx.py tests/test_cache_breakpoint.py tests/test_instructions.py --unit -q -p no:cacheprovider --basetemp=.prflow-tmp/pytest: 218 passed.The full
--unit --audio_eotCI gate was not run locally (livekit-serveris not installed). Provider integration and evaluation suites were not run.No live provider calls were made. Developed with AI assistance and independently reviewed.