ISSUE DESCRIPTION Subagent toolcalls not handled when using LiteLLM proxy Summary When…
- User
ISSUE DESCRIPTION
Subagent tool_calls not handled when using LiteLLM proxy
Summary
When using cagent with a LiteLLM proxy (OpenAI-compatible API), a subagent can return a valid
tool_callsresponse, but the host agent reports “no response from subagent” and does not execute the tool call.Environment
- cagent: v1.19.7
- OS: Linux (WSL2) x86_64
- LLM: gemini-pro via LiteLLM proxy (OpenAI-compatible)
Expected
Host agent receives the subagent response and executes the tool call(s), then continues the conversation with the tool result.
Actual
Host agent reports there is “no response from subagent” even though the subagent response contains a valid
tool_callsentry. The requested tool call is not executed.Minimal reproduction
- Configure cagent to use LiteLLM as the model provider (OpenAI-compatible API).
- Run a workflow where the subagent is asked to use tools.
- The subagent returns a tool call (e.g.,
company_searchwith JSON arguments) and finishes withfinish_reason: "stop". - The host agent logs “no response from subagent” and does not invoke the tool.
Notes
- The subagent response includes a valid tool call object (
type: function,name: company_search,arguments: {"datasources": ["confluence", "github", "gdrive"], "query": "LiteLLM usage teams integrations standards"}). - I can reproduce consistently with LiteLLM as the proxy in front of an OpenAI-compatible endpoint.
Please let me know if you want a sanitized example payload or a minimal agent config.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /docker-agent, current file: none) bash-$
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate relevant files related to subagent tool calls and LiteLLM proxy handling.
- Assistant
Let me get a better view of the repository structure.
- Assistant
This is a Go project. Let me look for files related to subagent tool_calls and LiteLLM proxy handling.
- Assistant
The string "no response from subagent" isn't directly in the source code. Let me search more broadly.
- Assistant
Let me examine the key files related to subagent handling.
- Assistant
Let me search for where the issue might be - tool calls handling and response processing:
- Assistant
Let me look at the model provider and chat files to understand the response handling:
- Assistant
Let me look at the chat package which likely handles tool call processing:
- Assistant
Let me continue reading the chat.go file to see the rest of the file.
- Assistant
Let me search for related terms:
- Assistant
Let me search for how tool calls are processed in the response stream:
- Assistant
Now I need to understand the issue better. The problem is about subagent tool_calls not being handled when using LiteLLM proxy. Let me search for the event stream adapter that handles the Chat Completions API (not the Responses API), since LiteLLM proxy typically uses the Chat Completions API.
- Assistant
Let me look at the runtime handling of tool calls to understand where the issue might be.
- Assistant
Let me look at the runtime loop to understand how tool calls are processed.
- Assistant
Let me look at more of the loop.go file, particularly around the streaming and tool call handling.
- Assistant
Let me look at the part of the file where tool calls are processed and the loop iteration.
- Assistant
Let me look for the streamResult type definition and how tool calls are collected from the stream:
- Assistant
Let me search for streamResult in the whole codebase.
- Assistant
Let me look at the streaming.go file to understand how tool calls are collected from the stream.
- Assistant
Let me read the main loop where tool calls are executed:
- Assistant
Now let me look at
GetLastAssistantMessageContentwhich is whatrunForwardinguses to get the subagent result: - Assistant
Let me look at the
GetLastAssistantMessageContentimplementation: - Assistant
Let me look at
getLastMessageContentByRole: - Assistant
Let me look at the implementation of getLastMessageContentByRole:
- Assistant
Now I see a potential issue!
GetLastAssistantMessageContent()only returns the text content, not tool calls. When a subagent returns only tool calls (with no text content), the function returns an empty string. Let me check what happens with empty results: - Assistant
Let me check the OpenAI adapter to understand how streaming responses are handled:
- Assistant
Let me look at the oaistream package:
- Assistant
Let me read the adapter.go file in oaistream package to understand how streaming responses are processed:
- Assistant
Now I'm getting closer to the issue! Let me look at the key part of the issue:
The issue says:
- "When using cagent with a LiteLLM proxy (OpenAI-compatible API), a subagent can return a valid
tool_callsresponse, but the host agent reports 'no response from subagent' and does not execute the tool call." - The subagent response includes a valid tool call object (
type: function,name: company_search,arguments: {...}).
Looking at the code, I see:
- In
agent_delegation.go:runForwarding, after the subagent finishes, it returns:
return tools.ResultSuccess(s.GetLastAssistantMessageContent()), nil-
GetLastAssistantMessageContent()returns the content of the last assistant message. -
In
loop.go:recordAssistantMessage, we have:
if strings.TrimSpace(res.Content) == "" && len(res.Calls) == 0 { slog.Debug("Skipping empty assistant message (no content and no tool calls)", "agent", a.Name()) return nil }This means that if the subagent response has ONLY tool calls and NO text content, the assistant message is NOT added to the session!
Then when
runForwardingcallss.GetLastAssistantMessageContent(), it finds no assistant message (because it was skipped), returns an empty string, and the tool result becomes:return tools.ResultSuccess(""), nilWhich is interpreted as an empty result. This is the "no response from subagent" behavior!
Let me verify this by looking at how the tool result is processed:
Let me check how the tool result is processed in the host agent's loop:
- "When using cagent with a LiteLLM proxy (OpenAI-compatible API), a subagent can return a valid
- Assistant
Let me search for the processToolCalls function.
- Assistant
Let me look at the tool_dispatch.go file:
- Assistant
Now let me search for the "no response from subagent" message more broadly:
- Assistant
Hmm, "no response from subagent" is not found in the code. The issue might be reporting a user-visible message that's not directly in the source code. Let me look at what
runForwardingreturns and how the caller uses it. The key is thatrunForwardingreturnstools.ResultSuccess(s.GetLastAssistantMessageContent())which returns the last assistant message's content text only.If the subagent's last message had tool_calls but no text content,
GetLastAssistantMessageContent()would return empty string. This would result in the tool result being empty, which the host agent might interpret as "no response".But wait, let me re-read the issue more carefully. The issue says:
- Host agent delegates a task to a subagent
- Subagent returns a response with tool_calls
- Host agent reports "no response from subagent"
The issue is that when using LiteLLM proxy (OpenAI-compatible), the subagent gets a response with tool_calls, but the host agent doesn't see those tool calls being executed.
Let me think about the flow:
- Host agent delegates to subagent via
runForwardingorrunCollecting - Subagent runs its own loop, which involves getting a response from the LLM
- If the LLM returns tool_calls, the subagent should execute them
- After the subagent finishes, the host agent gets the result
The issue might be in how the subagent's response is being handled. Let me look at how the subagent's stream is processed in
runForwarding:childEvents := r.RunStream(ctx, s) for event := range childEvents { evts.Emit(event) if errEvent, ok := event.(*ErrorEvent); ok { ... return nil, err } }And then:
return tools.ResultSuccess(s.GetLastAssistantMessageContent()), nilSo if the subagent's last message has only tool_calls and no text content, this would return empty.
But the real issue might be different. Let me think about this more carefully.
Looking at the
streaming.gocode, when LiteLLM returns a response with tool_calls, thehandleStreamfunction should correctly process them. The key is the condition:if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength {If LiteLLM returns
finish_reason: "stop"with tool calls, this code checks:if finishReason == chat.FinishReasonStop && len(toolCalls) > 0 { finishReason = chat.FinishReasonToolCalls } return streamResult{ Calls: toolCalls, ... Stopped: len(toolCalls) == 0, FinishReason: finishReason, }, nilSo
Stoppedwould befalsebecause there are tool calls, and the runtime loop should continue executing them.Actually wait, I need to re-read the issue more carefully. Let me look at the "Notes" section:
The subagent response includes a valid tool call object (type: function, name: company_search, arguments: {...}). I can reproduce consistently with LiteLLM as the proxy in front of an OpenAI-compatible endpoint.
So the subagent IS getting a response with tool_calls. But the host agent is reporting "no response from subagent".
Let me think about this differently. Maybe the issue is not about the subagent executing its own tool calls, but about the host agent receiving the subagent's response.
Actually, I think the issue might be in how the
runCollectingfunction handles the subagent's events. Let me look at it again:func (r *LocalRuntime) runCollecting(ctx context.Context, parent *session.Session, cfg SubSessionConfig, onContent func(string)) *agenttool.RunResult { ... var errMsg string events := r.RunStream(ctx, s) for event := range events { if ctx.Err() != nil { break } if choice, ok := event.(*AgentChoiceEvent); ok && choice.Content != "" { if onContent != nil { onContent(choice.Content) } } if errEvt, ok := event.(*ErrorEvent); ok { errMsg = errEvt.Error break } } ... if errMsg != "" { return &agenttool.RunResult{ErrMsg: errMsg} } result := s.GetLastAssistantMessageContent() parent.AddSubSession(s) return &agenttool.RunResult{Result: result} }This is the non-interactive path (background agents). The interactive path is
runForwarding:func (r *LocalRuntime) runForwarding(ctx context.Context, parent *session.Session, evts EventSink, req delegationRequest) (*tools.ToolCallResult, error) { ... childEvents := r.RunStream(ctx, s) for event := range childEvents { evts.Emit(event) if errEvent, ok := event.(*ErrorEvent); ok { ... return nil, err } } ... return tools.ResultSuccess(s.GetLastAssistantMessageContent()), nil }Both paths use
s.GetLastAssistantMessageContent()to get the result. If the subagent's last message has only tool_calls and no text content, this returns empty.But wait, the issue says the host agent reports "no response from subagent". This suggests the host agent is checking if the response is empty and reporting this message. Let me search for where this message might be generated.
Actually, the message "no response from subagent" might not be in the code. It might be a user-facing message generated by the host agent's logic. Let me think about what happens when
runForwardingreturns an empty result:runForwardingreturnstools.ResultSuccess("")(empty string)- This is returned to
handleTaskTransfer handleTaskTransferreturns this as the tool call result- The tool call result is processed by the runtime loop
Let me look at how the tool call result is processed. In
tool_dispatch.go, theprocessToolCallsfunction callsd.Processwhich handles the tool call execution.Actually, I think I need to look at the issue from a different angle. Let me re-read the issue:
Host agent reports there is "no response from subagent" even though the subagent response contains a valid tool_calls entry. The requested tool call is not executed.
So the host agent is NOT executing the tool call. This suggests the issue is in how the subagent's response is being processed.
Wait, I think I misunderstood the issue. Let me re-read it:
- The host agent delegates a task to a subagent
- The subagent calls the LLM and gets a response with tool_calls
- The subagent should execute those tool_calls and continue
- But the host agent reports "no response from subagent"
This suggests the subagent is not executing its tool calls. The issue is that when the subagent gets a response with tool_calls, something is preventing them from being executed.
Let me look at how the streaming adapter handles tool calls with LiteLLM proxy.
Actually, I think the issue might be in how LiteLLM sends the tool calls. Let me look at the OpenAI streaming adapter more carefully.
Looking at
oaistream/adapter.go:// Convert tool calls if present if len(choice.Delta.ToolCalls) > 0 { response.Choices[i].Delta.ToolCalls = make([]tools.ToolCall, len(choice.Delta.ToolCalls)) for j, toolCall := range choice.Delta.ToolCalls { id := toolCall.ID index := int(toolCall.Index) if existing, ok := a.toolCalls[index]; ok && id == "" { id = existing } else if id != "" { a.toolCalls[index] = id } response.Choices[i].Delta.ToolCalls[j] = tools.ToolCall{ ID: id, Type: tools.ToolType(toolCall.Type), Function: tools.FunctionCall{ Name: toolCall.Function.Name, Arguments: toolCall.Function.Arguments, }, } } }This looks correct. But wait, what if LiteLLM is not sending the tool calls in the streaming chunks? What if it's sending them differently?
Actually, let me look at how the
handleStreamfunction processes the finish reason. The key issue might be that LiteLLM sendsfinish_reason: "stop"even when there are tool calls, and thehandleStreamfunction might not be correctly detecting this.Looking at
streaming.go:if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { recordUsage() applyXMLFallback() finishReason := choice.FinishReason if finishReason == chat.FinishReasonStop && len(toolCalls) > 0 { finishReason = chat.FinishReasonToolCalls } return streamResult{ Calls: toolCalls, ... Stopped: len(toolCalls) == 0, FinishReason: finishReason, }, nil }So if LiteLLM sends
finish_reason: "stop"with tool calls, this should correctly setStopped: falseandFinishReason: "tool_calls". The runtime loop should then execute the tool calls.But wait, there's another issue. What if the finish reason is sent BEFORE the tool calls are fully accumulated? Let me look at how streaming chunks are processed.
Actually, I think I need to look at the issue from a different perspective. The issue says:
The subagent response includes a valid tool call object (type: function, name: company_search, arguments: {...}).
This suggests the subagent IS getting a complete tool call in its response. But the host agent is not seeing it.
Let me think about the flow again:
- Host agent calls
handleTaskTransferwhich callsrunForwarding runForwardingcallsr.RunStream(ctx, s)to run the subagent- The subagent runs its own loop, which involves: a. Getting tools b. Sending messages to the LLM c. Processing the LLM's response d. If the response has tool calls, executing them e. Looping back to step b
The issue might be in step c or d. Let me look at how the subagent's loop processes the LLM's response.
Looking at
loop.go, the main loop callshandleStreamto process the streaming response. If the response has tool calls, it callsprocessToolCallsto execute them.But wait, I think I see the issue now. Let me look at the
streaming.gocode again:for { response, err := stream.Recv() if errors.Is(err, io.EOF) { break } ... }After the loop ends (either by reaching EOF or by returning early), the code returns a
streamResult. But what if the stream ends BEFORE the tool calls are fully accumulated?Actually, I think the issue might be in how LiteLLM sends the streaming response. Let me think about this differently.
The issue says the subagent returns a valid tool call with
finish_reason: "stop". This suggests the LLM is returning a response with:- A tool call in the delta
finish_reason: "stop"
But the streaming adapter might not be correctly handling this case. Let me look at the OpenAI streaming adapter again.
Actually, I think I need to look at the issue from a completely different angle. Let me re-read the issue one more time:
When using cagent with a LiteLLM proxy (OpenAI-compatible API), a subagent can return a valid tool_calls response, but the host agent reports "no response from subagent" and does not execute the tool call.
So the host agent is reporting "no response from subagent". This is a specific error message. Let me search for where this message might be generated.
Actually, the message "no response from subagent" might not be in the code. It might be a user-facing message generated by the host agent's logic. Let me think about what happens when
runForwardingreturns an empty result.Wait, I think I see the issue now! Let me look at the
runForwardingfunction again:return tools.ResultSuccess(s.GetLastAssistantMessageContent()), nilThis returns the last assistant message's TEXT content. But if the subagent's last message has only tool calls and no text content, this returns an empty string.
But wait, the subagent should execute its tool calls and continue the loop. After executing the tool calls, the LLM should return a new response with text content. So the last assistant message should have text content.
Unless... the subagent is not executing its tool calls! Let me think about why this might happen.
Looking at the
streaming.gocode, when the LLM returns a response with tool calls, thehandleStreamfunction should correctly detect them and return astreamResultwithStopped: false. The runtime loop should then callprocessToolCallsto execute the tool calls.But what if the
handleStreamfunction is NOT correctly detecting the tool calls? Let me look at the code more carefully.Actually, I think I see the issue now! Let me look at the
streaming.gocode:if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... }This condition checks if the finish reason is "stop" or "length". But what if LiteLLM sends
finish_reason: "tool_calls"? In that case, this condition would NOT be true, and the code would continue processing the stream.But wait, the issue says the finish reason is "stop", not "tool_calls". So this should be handled correctly.
Let me look at the code after the main loop:
// After the loop ends (EOF or error) recordUsage() applyXMLFallback() ... return streamResult{ Calls: toolCalls, ... Stopped: stoppedDueToNoOutput, FinishReason: finishReason, }, nilSo if the stream ends with EOF, the code should return the accumulated tool calls. But what if the tool calls are not accumulated correctly?
Actually, I think I need to look at how the tool calls are accumulated. Let me look at the
handleStreamfunction more carefully.if len(choice.Delta.ToolCalls) > 0 { for _, delta := range choice.Delta.ToolCalls { idx, exists := toolCallIndex[delta.ID] if !exists { idx = len(toolCalls) toolCallIndex[delta.ID] = idx toolCalls = append(toolCalls, tools.ToolCall{ ID: delta.ID, Type: delta.Type, }) } ... } continue }This looks correct. The tool calls are accumulated based on their ID.
Hmm, let me think about this differently. Maybe the issue is not in how the tool calls are accumulated, but in how the streaming response is processed.
Actually, I think I might have found the issue! Let me look at the OpenAI streaming adapter again:
if a.trackUsage && (finishReasonStr == "stop" || finishReasonStr == "length") { finishReasonStr = "" }This line clears the finish reason when
trackUsageis true and the finish reason is "stop" or "length". This is done because the usage information is sent in the last chunk, and the finish reason needs to be preserved for that chunk.But what if this is causing the issue? If the finish reason is cleared, the
handleStreamfunction might not correctly detect the end of the stream.Actually, looking at the code more carefully, I see that the finish reason is preserved in
a.lastFinishReason:finishReason := chat.FinishReason(finishReasonStr) if finishReason != chat.FinishReasonNull && finishReason != "" { a.lastFinishReason = finishReason }And later, when the usage information is received:
if openaiResponse.JSON.Usage.Valid() { ... finishReason := a.lastFinishReason if finishReason == chat.FinishReasonNull || finishReason == "" { finishReason = chat.FinishReasonStop } ... }So the finish reason should be correctly preserved. But wait, what if the usage information is NOT received? In that case, the finish reason would be cleared, and the
handleStreamfunction might not correctly detect the end of the stream.Actually, I think I see the issue now! Let me look at the condition for processing tool calls:
if len(choice.Delta.ToolCalls) > 0 { ... continue }This
continuestatement skips the rest of the loop iteration. But what if the finish reason is sent in the SAME chunk as the tool calls? In that case, the finish reason would be skipped!Let me look at the streaming adapter code again:
for i := range openaiResponse.Choices { choice := &openaiResponse.Choices[i] finishReasonStr := choice.FinishReason ... finishReason := chat.FinishReason(finishReasonStr) ... response.Choices[i] = chat.MessageStreamChoice{ Index: int(choice.Index), FinishReason: finishReason, Delta: chat.MessageDelta{ Role: choice.Delta.Role, Content: choice.Delta.Content, }, } // Convert tool calls if present if len(choice.Delta.ToolCalls) > 0 { ... } }So the finish reason and tool calls are both included in the response. The
handleStreamfunction should process both.But wait, let me look at the
handleStreamfunction again:if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... return streamResult{...}, nil }This condition checks the finish reason FIRST. If the finish reason is "stop", it returns immediately, BEFORE processing the tool calls!
So if LiteLLM sends a chunk with BOTH
finish_reason: "stop"AND tool calls, the tool calls would be ignored!Let me verify this by looking at the order of operations in
handleStream:choice := response.Choices[0] // Check finish reason FIRST if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... return streamResult{...}, nil } // Track the provider's explicit finish reason if choice.FinishReason != "" { providerFinishReason = choice.FinishReason } // Handle tool calls AFTER checking finish reason if len(choice.Delta.ToolCalls) > 0 { ... }Yes! This is the bug! If the finish reason is "stop" and the tool calls are in the same chunk, the tool calls are ignored.
Actually wait, let me re-read the condition:
if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { recordUsage() applyXMLFallback() finishReason := choice.FinishReason if finishReason == chat.FinishReasonStop && len(toolCalls) > 0 { finishReason = chat.FinishReasonToolCalls } return streamResult{ Calls: toolCalls, ... Stopped: len(toolCalls) == 0, FinishReason: finishReason, }, nil }It checks
len(toolCalls) > 0, which is the ACCUMULATED tool calls from PREVIOUS chunks. So if the tool calls were sent in PREVIOUS chunks, they would be correctly detected.But what if the tool calls are sent in the SAME chunk as the finish reason? In that case,
toolCallswould be empty (because the tool calls haven't been processed yet), and the function would return with no tool calls!This is the bug! LiteLLM might be sending the tool calls and the finish reason in the same chunk, which causes the tool calls to be ignored.
Actually, I need to verify this. Let me look at the OpenAI streaming format. In the standard OpenAI API, tool calls are sent in separate chunks before the finish reason. But LiteLLM might be sending them differently.
Let me also check if there's a
continuestatement after processing tool calls:if len(choice.Delta.ToolCalls) > 0 { ... continue }Yes, there's a
continuestatement. So if the tool calls are processed, the loop continues to the next iteration, and the finish reason is processed in the next iteration.But if the finish reason and tool calls are in the SAME chunk, the finish reason is processed FIRST, and the tool calls are IGNORED.
This is the bug!
Actually, let me double-check by looking at the order of operations more carefully:
- Check if
choice.FinishReasonis "stop" or "length" - If yes, return immediately with the accumulated tool calls
- If no, check if there are tool calls in the delta
- If yes, accumulate them and continue
So the issue is that if the finish reason and tool calls are in the same chunk, the finish reason is processed first, and the tool calls are ignored.
The fix would be to process the tool calls BEFORE checking the finish reason.
Actually wait, I need to think about this more carefully. In the standard OpenAI API, the tool calls are sent in separate chunks, and the finish reason is sent in the last chunk (with empty delta). So the tool calls are accumulated BEFORE the finish reason is processed.
But LiteLLM might be sending them differently. If LiteLLM sends the tool calls and the finish reason in the same chunk, the tool calls would be ignored.
Let me verify this by looking at the OpenAI streaming format more carefully.
Actually, I think I need to look at the code more carefully. Let me look at the
handleStreamfunction again:for { response, err := stream.Recv() ... choice := response.Choices[0] if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... return streamResult{...}, nil } ... if len(choice.Delta.ToolCalls) > 0 { ... continue } ... }So the order is:
- Check finish reason
- If finish reason is "stop" or "length", return immediately
- Otherwise, process tool calls
If the finish reason and tool calls are in the same chunk, the finish reason is processed first, and the tool calls are ignored.
But wait, in the standard OpenAI API, the finish reason is NOT sent with the tool calls. The tool calls are sent in separate chunks, and the finish reason is sent in the last chunk (with empty delta).
So the issue is specific to LiteLLM, which might be sending the finish reason and tool calls in the same chunk.
The fix would be to process the tool calls BEFORE checking the finish reason. Or, more precisely, to check the finish reason AFTER processing the tool calls.
Actually, I think the fix is simpler. We should process the tool calls in the same chunk as the finish reason, and then return the result. Let me think about how to implement this.
The fix would be to move the tool call processing BEFORE the finish reason check:
// Handle tool calls FIRST if len(choice.Delta.ToolCalls) > 0 { ... continue } // Check finish reason AFTER processing tool calls if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... return streamResult{...}, nil }But wait, this would change the behavior for the standard OpenAI API. In the standard OpenAI API, the finish reason is sent in a separate chunk (with empty delta), so this change wouldn't affect it.
Actually, I think the fix is even simpler. We should process the tool calls in the same chunk as the finish reason, and then check if the finish reason is "stop" or "length". If it is, we return the result.
Let me implement this fix:
// Handle tool calls FIRST if len(choice.Delta.ToolCalls) > 0 { ... } // Check finish reason AFTER processing tool calls if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... return streamResult{...}, nil }But wait, this would change the behavior. In the original code, if the finish reason is "stop" and there are no tool calls, the function returns immediately. With this change, it would continue processing the chunk (which has no tool calls), and then return.
Actually, I think the original code is correct for the standard OpenAI API. The issue is specific to LiteLLM, which sends the finish reason and tool calls in the same chunk.
The fix should be to process the tool calls BEFORE checking the finish reason, but only if the finish reason is "stop" or "length". If the finish reason is something else (like "tool_calls"), we should process the tool calls and continue.
Actually, I think the simplest fix is to move the tool call processing BEFORE the finish reason check, and remove the
continuestatement. This way, the tool calls are always processed, and the finish reason is checked after.Let me implement this fix in
streaming.go.Actually, I just realized something. The issue might be even simpler. Let me look at the OpenAI streaming adapter again:
if a.trackUsage && (finishReasonStr == "stop" || finishReasonStr == "length") { finishReasonStr = "" }This clears the finish reason when
trackUsageis true. But what if the finish reason is "tool_calls"? In that case, the finish reason would NOT be cleared, and thehandleStreamfunction would process it correctly.But the issue says the finish reason is "stop", not "tool_calls". So the finish reason would be cleared, and the
handleStreamfunction might not correctly detect the end of the stream.Actually, looking at the code more carefully, I see that the finish reason is preserved in
a.lastFinishReason:finishReason := chat.FinishReason(finishReasonStr) if finishReason != chat.FinishReasonNull && finishReason != "" { a.lastFinishReason = finishReason }And later, when the usage information is received:
if openaiResponse.JSON.Usage.Valid() { ... finishReason := a.lastFinishReason ... }So the finish reason should be correctly preserved. But wait, what if the usage information is NOT received? In that case, the finish reason would be cleared, and the
handleStreamfunction might not correctly detect the end of the stream.Actually, I think the issue is in how the streaming adapter handles the finish reason. Let me look at the code more carefully.
OK, I think I've been overthinking this. Let me focus on the specific issue:
- LiteLLM sends a response with tool calls and
finish_reason: "stop" - The subagent's
handleStreamfunction processes the response - The host agent reports "no response from subagent"
The issue is that the subagent's
handleStreamfunction is not correctly processing the tool calls.Let me look at the
streaming.gocode one more time:if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { ... return streamResult{ Calls: toolCalls, ... Stopped: len(toolCalls) == 0, FinishReason: finishReason, }, nil }If the finish reason is "stop" and the tool calls are in the same chunk,
toolCallswould be empty (because the tool calls haven't been processed yet), and the function would return with no tool calls.This is the bug!
The fix is to process the tool calls BEFORE checking the finish reason.
Let me implement this fix.
Wait, actually I need to re-examine. Let me look at the condition again:
if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { recordUsage() applyXMLFallback() finishReason := choice.FinishReason if finishReason == chat.FinishReasonStop && len(toolCalls) > 0 { finishReason = chat.FinishReasonToolCalls } return streamResult{ Calls: toolCalls, Content: fullContent.String(), ReasoningContent: fullReasoningContent.String(), ThinkingSignature: thinkingSignature, ThoughtSignature: thoughtSignature, Stopped: len(toolCalls) == 0, // stop only when there are no tool calls to execute FinishReason: finishReason, Usage: messageUsage, }, nil }So if
toolCallsis empty (because the tool calls are in the same chunk but haven't been processed yet), the function returns withStopped: true(becauselen(toolCalls) == 0), which means the runtime loop would stop instead of executing the tool calls.This is the bug!
The fix is to process the tool calls BEFORE checking the finish reason.
Let me implement this fix now.
- Assistant
Now I can see the key issue. Let me look more closely at the flow. The problem is that in
handleStream, when a chunk hasFinishReason == "stop", it returns immediately BEFORE processing tool calls in that same chunk. But more importantly, the issue is about the subagent'stool_callsresponse not being handled. Let me look more carefully at the MCP server that says "No response from agent": - Assistant
Let me look at the MCP server to understand the context:
- Assistant
Now I see the MCP server uses
sess.GetLastAssistantMessageContent()and falls back to "No response from agent". Let me also look at thea2aadapter: - Assistant
Let me understand the streaming flow better. Let me look at where the OpenAI stream adapter is created:
- Assistant
Let me look at how
trackUsageis determined: - Assistant
Let me find where trackUsage is determined in the client:
- Assistant
Now I'm getting closer to understanding the issue. The key is in
handleStreaminstreaming.go. When a chunk contains BOTH tool_calls ANDfinish_reason: "stop"(which LiteLLM proxy may do), the finish_reason check fires BEFORE the tool_calls are accumulated, causing them to be lost. Let me verify this by looking at the exact code flow: - Assistant
Now I see the bug clearly! In
handleStream, the finish_reason check at line 133 fires BEFORE the tool_calls are accumulated at line 160. If a streaming chunk contains BOTHfinish_reason: "stop"ANDtool_callsin the delta (which LiteLLM proxy does), the tool calls are lost. Let me also check how the adapter handles this: - Assistant
Now I fully understand the bug. Let me trace through the issue:
-
With
trackUsage=true(default): When LiteLLM sends usage + tool_calls +finish_reason: "stop"in the same chunk, the adapter strips "stop" but then re-synthesizes it from the usage handling, settingChoices[0].FinishReason = "stop". ThenhandleStreamsees "stop" and returns before processing tool_calls in the delta. -
With
trackUsage=false: The "stop" finish_reason is preserved, same problem.
The fix is to move tool call processing BEFORE the finish_reason check in
handleStream. Let me first look at the existing tests to understand what's tested: -
- Assistant
Let me look at the existing streaming tests to understand the test patterns:
- Assistant
Let me search for streaming test files more broadly:
- Assistant
Let me look at the oaistream adapter test:
- Assistant
Let me also check if there's a test for
handleStream: - Assistant
Let me look for streaming test files in the runtime package:
- Assistant
Let me also look at the tool choice responses test:
- Assistant
Now I understand the bug. Let me create a test to reproduce the issue first:
- Assistant
Now let me implement the fix. The key change is to move tool call processing BEFORE the finish_reason check in
handleStream: - Assistant
Let me see the full tool call processing block:
- Assistant
Now I need to implement the fix. The approach is to move tool call processing BEFORE the finish_reason check, and use a flag to skip content processing when tool calls were handled. Let me make the edit:
- Assistant
Now let me make the edit to fix the issue. I need to move the tool call processing before the finish_reason check:
- Assistant
Let me use the
editcommand instead to make the changes directly. First, let me see the exact lines I need to modify: - Assistant
Now I need to replace the section from line 128 to 220 (where
continueis) with my new code. Let me see what comes after line 220: - Assistant
Now let me make the edit. I'll replace lines 128-212 with the fixed code:
- Assistant
Now let me check that the rest of the code is correct:
- Assistant
I see there are duplicate lines. The edit added the new code but the old code is still there. Let me fix this by removing the duplicate lines:
- Assistant
Let me verify the file is correct by looking at the whole function:
- Assistant
Good, the file looks correct now. Let me verify the key section of the change:
- Assistant
Good, the fix looks correct. Now let me run the existing tests to make sure nothing is broken:
- Assistant
Let me check the Go environment:
- Assistant
Let me check the GOPATH and GOMODCACHE settings:
- Assistant
Let me check if there's a go environment setup script:
- Assistant
Let me set up the environment properly.
- Assistant
Let me run the full runtime tests:
- Assistant
The runtime tests are failing, but these look like pre-existing failures (context window overflow errors). Let me check if these are pre-existing:
- Assistant
Let me check which specific test is failing:
- Assistant
Let me check which tests actually fail:
- Assistant
Let me check if this test was failing before my change:
- Assistant
Good, the test was already failing before my change. Let me restore my changes:
- Assistant
Now let me run the oaistream adapter tests:
- Assistant
Good, all oaistream tests pass. Now let me also check that the code compiles correctly:
- Assistant
Good, the code compiles. Let me also run the full test suite for the runtime package (excluding the pre-existing failure):
- Assistant
All tests pass (except the pre-existing failure). Let me also run a broader set of tests to make sure nothing else is broken:
- Assistant
Let me check if those failures are pre-existing:
- Assistant
Good, the
TestDoCompactAfterHookFiresfailure is pre-existing. Let me restore my changes: - Assistant
Now let me also check if there's an issue in the adapter that might need fixing too. Let me look at the adapter's handling of the usage chunk when tool_calls are present:
- Assistant
Now let me also consider if there's an issue in the adapter itself. When
trackUsageis true and LiteLLM sends usage + tool_calls +finish_reason: "stop"in the same chunk:- The adapter strips "stop" (since
trackUsageis true) - Tool calls are populated in the delta
- Usage handling re-synthesizes "stop" as the finish reason
- Returns with BOTH
FinishReason: "stop"ANDToolCalls
With my fix in
handleStream, this is now handled correctly because tool calls are accumulated BEFORE the finish reason check. But I should also consider if the adapter should be smarter about this. Let me check if there's a case where the adapter strips "stop" but the usage chunk comes separately (standard OpenAI behavior): - The adapter strips "stop" (since
- Assistant
Good, the adapter's behavior is fine - it correctly passes through tool_calls. The issue is solely in
handleStream. Now let me also run the full build to make sure everything compiles: - Assistant
Good, everything compiles. Let me now look at the final diff to verify my changes:
- Assistant
The diff looks correct. The key changes are:
- Tool call processing moved BEFORE the finish_reason check
handledToolCallsflag used to skip content processing when tool calls were in the chunk- The
continuestatement removed (replaced by the flag-based approach)
Let me now run the full test suite one more time to make sure everything is working:
- Assistant
Let me check if any of those tests failed:
- Assistant
Let me fix the grep command:
- Assistant
All tests pass. Let me also run the oaistream tests to make sure they still pass:
- Assistant
All tests pass. Now let me verify my understanding of the issue by looking at the full picture one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →