CopilotKit / CopilotKit/CopilotKit
AG-UI: tool result messages missing from conversation history on subsequent turns (causes LLM 400)
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 37.4k
- Forks
- 4.6k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 444
Description
♻️ Reproduction Steps
- Set up a CopilotKit v2
<CopilotChat>with an AG-UIHttpAgentbackend that has server-side tools (e.g., MCP tools) - Send a message that triggers a tool call (e.g., "What's the weather in Seattle?")
- Agent streams:
TOOL_CALL_START→TOOL_CALL_ARGS→TOOL_CALL_END→TOOL_CALL_RESULT→TEXT_MESSAGE_*→RUN_FINISHED - First turn completes successfully — user sees the tool result and text response
- Send a second message (e.g., "What is 5 * 33?")
- Backend receives the conversation history and returns 400 Bad Request from the LLM provider
Root Cause
When CopilotKit replays the conversation history on the second turn, it sends the messages array to the AG-UI backend without the tool result message. The history looks like:
user: "What's the weather in Seattle?"
assistant: [tool_calls: [{id: "call_abc", function: {name: "get_weather", ...}}]]
assistant: "The weather in Seattle is 15°C and partly cloudy."
user: "What is 5 * 33?"
The correct history should be:
user: "What's the weather in Seattle?"
assistant: [tool_calls: [{id: "call_abc", function: {name: "get_weather", ...}}]]
tool: {tool_call_id: "call_abc", content: "Seattle: 15°C, partly cloudy"}
assistant: "The weather in Seattle is 15°C and partly cloudy."
user: "What is 5 * 33?"
The tool result message for call_abc is completely absent. This causes both OpenAI and Azure OpenAI to reject the request with a 400 error because tool_calls in the assistant message have no corresponding tool result.
✅ Expected Behavior
CopilotKit should persist TOOL_CALL_RESULT events in its internal conversation state and include the corresponding tool role messages when replaying history to the backend on subsequent turns.
❌ Actual Behavior
Tool result messages are dropped from the conversation state. On the second turn, the backend receives assistant(tool_calls) followed directly by assistant(text) with no tool(result) in between. The LLM provider (OpenAI/Azure OpenAI) rejects this with a 400 error.
Error from Azure OpenAI Responses API:
Code: agent_run_error_event
Message: An internal error has occurred while streaming events.
The underlying cause is a 400 from the LLM because the message history violates the constraint that every tool_calls entry must have a matching tool result message.
Workaround
We subclass AgentFrameworkAgent on the backend to inject synthetic tool result dicts into the raw messages before they enter the library pipeline:
class ToolGapFixAgent(AgentFrameworkAgent):
async def run(self, input_data):
messages = input_data.get("messages")
if messages:
input_data = {**input_data, "messages": fix_tool_gaps(messages)}
async for event in super().run(input_data):
yield event
Where fix_tool_gaps scans for assistant messages with tool_calls not followed by a tool result, and injects synthetic ones.
Environment
@copilotkit/react-core: 1.55.3
@ag-ui/client: 0.0.52
agent-framework-ag-ui: 1.0.0b260311 (Python backend)
LLM provider: Azure OpenAI (Responses API)
Transport: AG-UI HttpAgent via agents__unsafe_dev_only prop
Frontend Setup
const agent = new HttpAgent({ url: "/ag-ui" });
<CopilotKit
runtimeUrl="/ag-ui"
agent="my-agent"
agents__unsafe_dev_only={{ "my-agent": agent }}
useSingleEndpoint={false}
>
<CopilotChat />
</CopilotKit>
Related Issues
- #2504 —
tool_useids withouttool_resultblocks (different root cause but same symptom) - #3470 — tool calls lost in single turn dedup (UI-side, different bug)
- #3462 — sub-agent tool calls disappear from UI after completion
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the second-turn failure with CopilotChat, an AG-UI HttpAgent, and a server-side tool, then inspect conversation replay around TOOL_CALL_RESULT events. Verify that the replayed history includes a matching tool-role result between the assistant tool call and its text response, and confirm that a subsequent request no longer receives a 400 from the LLM provider.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- api, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100