Now I understand the issue fully. Let me trace through the code:
-
When a subagent's model returns tool calls with finish_reason: "stop" (which LiteLLM may do for Gemini), the handleStream function in streaming.go correctly handles this:
- Line 136-139: If
finishReason == "stop" and len(toolCalls) > 0, it sets finishReason = "tool_calls" and Stopped = false
- Tool calls are returned in
res.Calls
-
The loop in loop.go then processes these tool calls via processToolCalls().
-
However, when the subagent finishes and runForwarding returns tools.ResultSuccess(s.GetLastAssistantMessageContent()):
GetLastAssistantMessageContent() calls getLastMessageContentByRole() (line 545)
getLastMessageContentByRole() only returns the Content field, NOT the ToolCalls field (line 578)
-
If the last assistant message has tool calls but NO text content, it returns empty string.
-
The MCP server/A2A code then displays "No response from agent" (line 196 in server.go).
The fix should modify getLastMessageContentByRole to return a JSON representation of tool calls when text content is empty but tool calls are present.
Wait, but I need to think about this more carefully. The issue says "Subagent tool_calls not handled when using LiteLLM proxy". Let me re-read the issue:
- The subagent returns a tool call (e.g.,
company_search with JSON arguments) and finishes with finish_reason: "stop".
- The host agent logs "no response from subagent" and does not invoke the tool.
So the issue is that:
- The subagent returns tool calls with finish_reason: "stop"
- The host agent gets "no response from subagent"
- The tool call is not executed
This could mean two things:
a) The subagent didn't execute the tool call (because finish_reason was "stop", not "tool_calls")
b) The subagent executed the tool call, but the host agent didn't receive the result
Looking at the handleStream function, when finish_reason is "stop" but tool calls are present:
- Line 136-139:
finishReason = chat.FinishReasonToolCalls and Stopped = len(toolCalls) == 0 (which is false when tool calls are present)
So the loop should continue and process the tool calls. The subagent should execute the tool calls.
But wait, the issue says "the host agent logs 'no response from subagent'". This message comes from GetLastAssistantMessageContent() returning empty string.
So the question is: why would the subagent's last assistant message have empty content?
If the subagent's loop processed the tool calls correctly, the last assistant message would be the NEXT model response after the tool results, which should have text content.
Unless... the subagent's loop didn't process the tool calls. This could happen if:
- The stream didn't accumulate the tool calls (LiteLLM format issue)
- The handleStream function returned empty tool calls
Let me check the oaistream adapter to see how it handles tool calls:
The oaistream adapter processes tool calls from the stream delta. If LiteLLM returns tool calls in a non-standard format, the adapter might not accumulate them.
But actually, the issue says "The subagent response includes a valid tool call object". So the tool calls ARE present in the response.
The issue must be that the handleStream function doesn't accumulate the tool calls properly. Let me check if there's a specific issue with how LiteLLM formats tool calls.
Actually, looking at the issue more carefully:
3. The subagent returns a tool call (e.g., company_search with JSON arguments) and finishes with finish_reason: "stop".
The subagent's model returned tool calls AND finish_reason was "stop". In the handleStream function, when finish_reason is "stop" and there are tool calls:
- Line 136-139:
finishReason = chat.FinishReasonToolCalls and Stopped = false
So the tool calls should be processed. Unless the tool calls were empty.
I think the issue is that LiteLLM might return tool calls in a format that the oaistream adapter doesn't handle correctly. The adapter expects tool calls to be in choice.Delta.ToolCalls, but LiteLLM might format them differently.
Let me check the oaistream adapter more carefully:
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
}
The adapter processes tool calls from choice.Delta.ToolCalls. If LiteLLM returns tool calls in a different format (e.g., as part of the Content field), this won't work.
But wait, LiteLLM is supposed to convert Gemini responses to OpenAI format. If the Gemini model returned tool calls, LiteLLM should include them in Delta.ToolCalls.
Unless there's a bug in LiteLLM or in the oaistream adapter.
Actually, I think I've been overcomplicating this. Let me re-read the issue one more time:
- The subagent returns a tool call (e.g.,
company_search with JSON arguments) and finishes with finish_reason: "stop".
- The host agent logs "no response from subagent" and does not invoke the tool.
The key insight is:
- The subagent returns tool calls with finish_reason: "stop"
- The host agent gets "no response from subagent"
If the subagent's loop processed the tool calls correctly, the last assistant message would have text content (from the model's response after the tool results). So the issue must be that the subagent's loop didn't process the tool calls.
The handleStream function correctly handles finish_reason: "stop" with tool calls. So the issue must be that the tool calls weren't accumulated.
This could be a LiteLLM issue where tool calls are returned in a non-standard format. But the cagent code should be robust enough to handle this.
Actually, let me re-read the handleStream function one more time:
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(),
...
Stopped: len(toolCalls) == 0,
...
}, nil
}
When finish_reason is "stop" and there are tool calls:
finishReason is corrected to chat.FinishReasonToolCalls
Stopped is false (because len(toolCalls) == 0 is false when there are tool calls)
- Tool calls are returned
So the tool calls should be processed. Unless len(toolCalls) == 0.
This means the oaistream adapter didn't accumulate the tool calls. This could be because LiteLLM returns tool calls in a format that the adapter doesn't handle.
Actually, wait. Let me look at the oaistream adapter more carefully:
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 {
// ...
}
}
The adapter checks choice.Delta.ToolCalls. If LiteLLM returns tool calls in a different format (e.g., as a string in the Content field), this won't work.
But LiteLLM is supposed to return OpenAI-compatible responses. If the model returned tool calls, LiteLLM should include them in the delta.
Unless LiteLLM returns tool calls in a non-standard way for certain models (like Gemini).
OK, I think I've been overcomplicating this. Let me focus on the fix.
The issue is that when the subagent's last assistant message has tool calls but no text content, GetLastAssistantMessageContent() returns empty string. The fix should modify getLastMessageContentByRole to return a JSON representation of tool calls when text content is empty but tool calls are present.
But wait, there's another possibility. What if the issue is that the handleStream function returns empty tool calls when finish_reason is "stop"?
Looking at the handleStream function:
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,
...
}, nil
}
This returns when finish_reason is "stop". But what if finish_reason is "stop" BEFORE the tool calls are fully accumulated?
In streaming, the model returns:
- A message with role "assistant" and some tool call deltas
- A message with finish_reason: "stop" (instead of "tool_calls")
If the finish_reason is sent BEFORE all tool call deltas are accumulated, the handleStream function returns early with incomplete tool calls.
This is the issue! When finish_reason is "stop" and tool calls are still being streamed, the handleStream function returns early with incomplete (or empty) tool calls.
Actually, wait. In OpenAI streaming, the finish_reason is sent in the LAST chunk, after all content/tool calls are sent. So the tool calls should be fully accumulated before finish_reason is "stop".
But LiteLLM might not follow this convention. LiteLLM might send finish_reason: "stop" before all tool call deltas are sent.
Actually, looking at the issue again:
3. The subagent returns a tool call (e.g., company_search with JSON arguments) and finishes with finish_reason: "stop".
This says the subagent's model returned tool calls AND finish_reason: "stop". The tool calls are present in the response. So the oaistream adapter should have accumulated them.
Unless the tool calls are present but the handleStream function doesn't return them.
Let me look at the handleStream function one more time:
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
// ...
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
continue
}
// ...
if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength {
// Return early
}
}
The loop processes tool calls with continue, so it doesn't return early when tool calls are present. It only returns when finish_reason is "stop" (or "length").
So if finish_reason is "stop" and tool calls were accumulated, they should be returned. Unless the tool calls weren't accumulated.
I think the issue is that LiteLLM returns tool calls in a format that the oaistream adapter doesn't handle. For example, LiteLLM might return tool calls as part of the Content field (as XML tags) instead of as Delta.ToolCalls.
Let me check if there's a code path for this in the handleStream function:
applyXMLFallback := func() {
if len(toolCalls) > 0 {
return
}
extracted, textBefore, found := extractXMLToolCalls(fullContent.String())
if !found {
return
}
toolCalls = extracted
fullContent.Reset()
fullContent.WriteString(textBefore)
for _, tc := range toolCalls {
toolDef := toolDefMap[tc.Function.Name]
events.Emit(PartialToolCall(tc, toolDef, a.Name()))
}
}
The applyXMLFallback function extracts tool calls from XML tags in the content. This is a fallback for when tool calls are returned as XML instead of Delta.ToolCalls.
So if LiteLLM returns tool calls as XML tags in the Content field, the fallback should extract them.
But wait, applyXMLFallback is called in two places:
- When finish_reason is "stop" (line 135)
- After the stream ends (line 190)
If finish_reason is "stop" and tool calls are in the Content field as XML, the fallback should extract them.
Unless the tool calls are not in the Content field as XML, and they're also not in Delta.ToolCalls. In this case, the fallback won't help.
OK, I think the issue is that LiteLLM returns tool calls in a format that the cagent code doesn't handle. The tool calls might be:
- In Delta.ToolCalls (standard OpenAI format) - handled
- As XML tags in the Content field - handled by fallback
- In some other format - NOT handled
For case 3, the tool calls are not accumulated, and len(toolCalls) == 0. So the handleStream function returns:
Calls: nil (empty)
Content: "" (empty, if no text content)
Stopped: true (because len(toolCalls) == 0)
This means the loop exits without processing tool calls, and the subagent's last assistant message has no content.
The fix should modify getLastMessageContentByRole to return a JSON representation of tool calls when text content is empty but tool calls are present.
But wait, if the tool calls weren't accumulated, there are no tool calls to return. So this fix won't help.
Unless... the fix is to accumulate tool calls from the Content field (as XML or JSON) when they're not in Delta.ToolCalls.
Actually, looking at the issue description again:
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"}).
This suggests the tool calls ARE present in the response. So the oaistream adapter should have accumulated them.
Unless the tool calls are present in the response but the oaistream adapter didn't process them correctly.
Let me look at the oaistream adapter one more time:
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. The adapter processes tool calls from Delta.ToolCalls.
Unless LiteLLM returns tool calls in a format that uses different field names. For example, LiteLLM might use function_call instead of tool_calls, or use a different structure for the function name and arguments.
Actually, I just realized something. The oaistream adapter processes choice.Delta.ToolCalls. But if LiteLLM returns tool calls as part of choice.Delta.FunctionCall (legacy OpenAI format), the adapter might not process them.
Let me check:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
The adapter also processes choice.Delta.FunctionCall. So tool calls in the legacy format should be handled.
But wait, the adapter returns response.Choices[i].Delta.FunctionCall, not response.Choices[i].Delta.ToolCalls. So the tool calls in legacy format might not be processed by the handleStream function.
Let me check the handleStream function:
// Handle tool calls
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
continue
}
The handleStream function only checks choice.Delta.ToolCalls, not choice.Delta.FunctionCall. So tool calls in legacy format won't be processed.
This could be the issue! If LiteLLM returns tool calls in legacy format (FunctionCall instead of ToolCalls), the handleStream function won't process them.
Actually, looking at the oaistream adapter more carefully:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
The adapter sets Delta.FunctionCall, but the handleStream function only checks Delta.ToolCalls. So tool calls in legacy format won't be accumulated.
This is the bug! The oaistream adapter processes FunctionCall but doesn't convert it to ToolCalls. The handleStream function expects ToolCalls, not FunctionCall.
Actually, let me check the chat.MessageDelta struct:
type MessageDelta struct {
Role MessageRole `json:"role"`
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content,omitempty"`
ThinkingSignature string `json:"thinking_signature,omitempty"`
ThoughtSignature []byte `json:"thought_signature,omitempty"`
FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
So MessageDelta has both FunctionCall and ToolCalls. The oaistream adapter sets FunctionCall for legacy format, and ToolCalls for the new format.
But the handleStream function only checks Delta.ToolCalls:
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
}
So tool calls in legacy format (FunctionCall) won't be processed. This is the bug!
Actually, wait. Let me check if the oaistream adapter converts FunctionCall to ToolCalls:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
No, the adapter doesn't convert FunctionCall to ToolCalls. It only sets FunctionCall.
So if LiteLLM returns tool calls in legacy format (FunctionCall), the handleStream function won't process them because it only checks ToolCalls.
This could be the issue. LiteLLM might return Gemini tool calls in a non-standard format that the adapter doesn't handle correctly.
Actually, I think the issue might be simpler. Let me check if LiteLLM returns tool calls in a format that the adapter can handle.
Looking at the oaistream adapter:
// 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 {
// ...
}
}
The adapter processes tool calls from choice.Delta.ToolCalls. If LiteLLM returns tool calls in this format, they should be processed.
But what if LiteLLM returns tool calls as part of choice.Delta.Content? For example, LiteLLM might serialize tool calls as JSON strings in the Content field.
In this case, the adapter would accumulate the tool call JSON in fullContent, but not in toolCalls. Then applyXMLFallback would be called, but it only handles XML tags, not JSON strings.
So if LiteLLM returns tool calls as JSON strings in the Content field, the handleStream function won't process them.
OK, I think I've spent enough time analyzing the issue. Let me focus on the fix.
The issue is that when a subagent returns tool calls but no text content, GetLastAssistantMessageContent() returns empty string, causing "No response from agent".
The fix should modify getLastMessageContentByRole to return a JSON representation of tool calls when text content is empty but tool calls are present. This will ensure that even if the subagent's last message contains tool calls but no text content, the host agent will receive meaningful content.
Actually, wait. I just realized that this fix won't help if the tool calls weren't accumulated. If the tool calls are not in the session's last assistant message, this fix won't help.
But the issue says "The subagent response includes a valid tool call object". So the tool calls ARE present in the response. They should be in the session's last assistant message.
So the fix should work.
Let me also consider edge cases:
- What if the last assistant message has both text content AND tool calls?
- The fix should return the text content (not the tool calls), because text content is more meaningful.
- What if the last assistant message has neither text content NOR tool calls?
- The fix should return empty string (no change).
- What if the last assistant message has tool calls but no text content?
- The fix should return a JSON representation of the tool calls.
This seems correct. Let me implement the fix.
Actually, thinking about it more, there's a potential issue. When a subagent's model returns tool calls with finish_reason: "stop", the handleStream function:
- Correctly detects the tool calls and sets Stopped: false
- Returns the tool calls in res.Calls
Then the loop:
- Calls processToolCalls to execute the tool calls
- Feeds results back to the model
- Continues the loop
So the subagent should process the tool calls and continue. The last assistant message from the subagent would be the response AFTER the tool calls are executed, which should have text content.
But the issue says the host agent gets "no response from subagent". This means the subagent's last assistant message has no text content.
This could happen if:
- The tool calls weren't accumulated (LiteLLM format issue)
- The handleStream function didn't return the tool calls
- The loop didn't process the tool calls
- The model returned an empty response after the tool calls
For case 1, if LiteLLM returns tool calls in a format that the adapter doesn't handle, the tool calls won't be accumulated.
For case 2, the handleStream function should return the tool calls if they were accumulated. Unless there's a bug.
For case 3, the loop should process the tool calls. Unless there's a bug.
For case 4, the model returned an empty response, which is unlikely.
I think case 1 is most likely. LiteLLM returns tool calls in a format that the adapter doesn't handle.
But the fix I proposed (modifying getLastMessageContentByRole to return tool calls as JSON) won't help if the tool calls weren't accumulated.
Hmm, let me think about this differently. The issue says:
The subagent returns a tool call (e.g., company_search with JSON arguments) and finishes with finish_reason: "stop".
This suggests the subagent's model returned tool calls. The handleStream function should have accumulated them (if LiteLLM returns them in a standard format).
If the tool calls were accumulated, the handleStream function should return them:
return streamResult{
Calls: toolCalls,
...
Stopped: len(toolCalls) == 0,
...
}
Then the loop should process them:
stopRun, stopMsg := r.processToolCalls(ctx, sess, res.Calls, agentTools, events)
So the tool calls should be executed. The subagent's last assistant message would be the response AFTER the tool calls are executed, which should have text content.
Unless... the subagent's model returned an empty response after the tool calls are executed. In this case, the last assistant message would have no content.
But this is unlikely. The model should always return something.
OK, I think the issue is simpler than I thought. Let me re-read the issue one more time:
- The subagent returns a tool call (e.g.,
company_search with JSON arguments) and finishes with finish_reason: "stop".
- The host agent logs "no response from subagent" and does not invoke the tool.
I think the issue is that the subagent's model returned tool calls with finish_reason: "stop". In this case:
- The handleStream function returns early with the tool calls (if they were accumulated)
- The loop processes the tool calls
- The subagent continues with the tool results
But the issue says "the host agent logs 'no response from subagent'". This means the subagent's last assistant message has no text content.
This could happen if:
- The tool calls weren't accumulated (LiteLLM format issue)
- The model returned an empty response after the tool calls are executed
For case 1, if LiteLLM returns tool calls in a format that the adapter doesn't handle, the tool calls won't be accumulated. Then len(toolCalls) == 0, and the handleStream function returns Stopped: true.
In this case, the loop doesn't process the tool calls, and the subagent's last assistant message has no content (because the tool calls weren't accumulated).
This is the bug! The fix should ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format.
But actually, looking at the handleStream function 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(),
...
Stopped: len(toolCalls) == 0,
...
}, nil
}
When finish_reason is "stop":
applyXMLFallback is called (to extract tool calls from XML tags in the content)
- If
len(toolCalls) > 0, finish_reason is corrected to "tool_calls"
Stopped is len(toolCalls) == 0
So if tool calls are accumulated (either from Delta.ToolCalls or from XML fallback), they should be returned.
If tool calls are NOT accumulated (because LiteLLM returns them in a non-standard format), then:
len(toolCalls) == 0
Stopped = true
- The loop exits without processing tool calls
This is the bug. The fix should ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format.
But actually, if the tool calls weren't accumulated, there's no way to extract them from the response. The content field might contain the tool calls as JSON strings, but the handleStream function doesn't parse them.
Actually, looking at the issue description again:
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"}).
This suggests the tool calls ARE present in the response. They should be extractable from the response.
Let me think about where the tool calls might be in the response:
- In Delta.ToolCalls - handled by the adapter
- As XML tags in the Content field - handled by applyXMLFallback
- As JSON strings in the Content field - NOT handled
If LiteLLM returns tool calls as JSON strings in the Content field, the adapter will accumulate them in fullContent. But the handleStream function won't extract them.
So the fix should modify applyXMLFallback (or add a new fallback) to also extract tool calls from JSON strings in the content.
Actually, looking at applyXMLFallback:
applyXMLFallback := func() {
if len(toolCalls) > 0 {
return
}
extracted, textBefore, found := extractXMLToolCalls(fullContent.String())
if !found {
return
}
toolCalls = extracted
fullContent.Reset()
fullContent.WriteString(textBefore)
for _, tc := range toolCalls {
toolDef := toolDefMap[tc.Function.Name]
events.Emit(PartialToolCall(tc, toolDef, a.Name()))
}
}
This only extracts tool calls from XML tags. If LiteLLM returns tool calls as JSON strings, this won't help.
The fix should add a new fallback to extract tool calls from JSON strings in the content.
But actually, I'm not sure if LiteLLM returns tool calls as JSON strings in the Content field. Let me check the issue description again:
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"}).
This shows the tool call object. But it doesn't tell me where it's located in the response.
OK, I think I've spent enough time analyzing the issue. Let me focus on the fix.
The issue is that GetLastAssistantMessageContent() returns empty string when the last assistant message has no text content. If the last assistant message has tool calls but no text content, the fix should return a JSON representation of the tool calls.
But actually, if the tool calls weren't accumulated (because LiteLLM returns them in a non-standard format), there are no tool calls to return. So this fix won't help.
I think the real fix should be to ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format.
But I'm not sure what format LiteLLM uses. The safest fix would be to:
- Modify
applyXMLFallback (or add a new fallback) to also extract tool calls from JSON strings in the content
- Modify
getLastMessageContentByRole to return tool calls as JSON when text content is empty
Let me implement both fixes.
Actually, wait. Let me re-read the issue one more time:
- The subagent returns a tool call (e.g.,
company_search with JSON arguments) and finishes with finish_reason: "stop".
- The host agent logs "no response from subagent" and does not invoke the tool.
The issue says "the host agent logs 'no response from subagent'". This message comes from GetLastAssistantMessageContent() returning empty string.
So the fix should be to modify GetLastAssistantMessageContent() to return a non-empty string when the last assistant message has tool calls but no text content.
But if the tool calls weren't accumulated, there are no tool calls to return. So the fix won't help.
Unless... the tool calls are accumulated but the loop didn't process them. In this case, the last assistant message would have the tool calls.
Let me think about this scenario:
- The model returns tool calls with finish_reason: "stop"
- The handleStream function accumulates the tool calls
- The handleStream function returns with
Stopped: false (because len(toolCalls) > 0)
- The loop processes the tool calls
- The tool results are fed back to the model
- The model returns a response
If step 6 returns an empty response, the last assistant message would have no content. But this is unlikely.
Unless the loop doesn't continue to step 4-6. Let me check the loop code:
stopRun, stopMsg := r.processToolCalls(ctx, sess, res.Calls, agentTools, events)
// ...
if res.Stopped {
// ...
return turnExit
}
The loop checks res.Stopped AFTER processing the tool calls. So the tool calls should be processed before checking if the loop should stop.
If res.Stopped is false (because tool calls were accumulated), the loop continues to the next iteration.
But wait, what if res.Calls is empty (tool calls weren't accumulated)? Then processToolCalls is called with empty calls, and nothing happens. Then res.Stopped is true (because len(toolCalls) == 0), and the loop exits.
In this case, the last assistant message has no content (because the tool calls weren't accumulated).
This is the bug. The tool calls weren't accumulated, so the loop exits without processing them.
The fix should ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format.
But I'm not sure what format LiteLLM uses. Let me check if there's a pattern.
Looking at the oaistream adapter, it processes tool calls from Delta.ToolCalls. If LiteLLM returns tool calls in a different format (e.g., as FunctionCall), the adapter might not process them.
Let me check if the oaistream adapter converts FunctionCall to ToolCalls:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
The adapter sets FunctionCall, but not ToolCalls. So tool calls in FunctionCall format won't be processed by the handleStream function.
This could be the bug! If LiteLLM returns tool calls in FunctionCall format (legacy OpenAI format), the adapter sets Delta.FunctionCall, but the handleStream function only checks Delta.ToolCalls.
The fix should modify the oaistream adapter to also set Delta.ToolCalls when Delta.FunctionCall is present.
Let me implement this fix.
Actually, I just realized something. The handleStream function has this code:
// Handle tool calls
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
continue
}
This only checks Delta.ToolCalls. If LiteLLM returns tool calls as Delta.FunctionCall, the handleStream function won't process them.
So the fix should be to modify the oaistream adapter to convert FunctionCall to ToolCalls.
Let me implement this fix.
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
// Set both FunctionCall (for backward compatibility) and ToolCalls
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
// Also set ToolCalls for consistency with handleStream
response.Choices[i].Delta.ToolCalls = append(response.Choices[i].Delta.ToolCalls, tools.ToolCall{
Type: "function",
Function: tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
},
})
}
This way, tool calls in FunctionCall format will also be processed by the handleStream function.
But wait, I need to be careful about the tool call ID. FunctionCall doesn't have an ID, but ToolCall does. The handleStream function expects tool calls to have IDs.
Looking at the handleStream function:
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,
})
}
// ...
}
}
The handleStream function expects tool calls to have IDs. If the ID is empty, the tool call won't be properly tracked.
So if LiteLLM returns tool calls in FunctionCall format (without ID), the tool calls won't be properly tracked by the handleStream function.
This is getting complicated. Let me step back and think about the simplest fix.
Actually, let me re-read the issue one more time:
- The subagent returns a tool call (e.g.,
company_search with JSON arguments) and finishes with finish_reason: "stop".
- The host agent logs "no response from subagent" and does not invoke the tool.
I think the issue is simpler than I thought. The subagent's model returns tool calls with finish_reason: "stop". The handleStream function correctly handles this (sets finishReason to "tool_calls" and Stopped to false). The loop should process the tool calls.
But the issue says "the host agent logs 'no response from subagent'". This means the subagent's last assistant message has no text content.
If the subagent processed the tool calls correctly, the last assistant message would have text content (from the model's response after the tool results). So the issue must be that the tool calls weren't processed.
This could happen if:
- The tool calls weren't accumulated (LiteLLM format issue)
- The loop didn't process the tool calls (bug in the loop)
- The model returned an empty response after the tool results
For case 3, this is unlikely. The model should always return something.
For case 1 or 2, there's a bug. Let me check if there's a bug in the loop.
Looking at the loop code:
res, usedModel, err := r.fallback.execute(streamCtx, a, model, messages, agentTools, sess, m, events)
// ...
stopRun, stopMsg := r.processToolCalls(ctx, sess, res.Calls, agentTools, events)
// ...
if res.Stopped {
// ...
return turnExit
}
The loop calls processToolCalls with res.Calls. If res.Calls is empty (tool calls weren't accumulated), nothing happens. Then the loop checks res.Stopped. If res.Stopped is true (because tool calls weren't accumulated), the loop exits.
So if the tool calls weren't accumulated, the loop exits without processing them. This is the bug.
The fix should ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format.
But I'm not sure what format LiteLLM uses. Let me check if there's a pattern.
Actually, let me look at the oaistream adapter one more time:
// 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,
},
}
}
}
The adapter processes tool calls from choice.Delta.ToolCalls. If LiteLLM returns tool calls in this format, they should be processed.
But what if LiteLLM returns tool calls in a different format? For example, LiteLLM might return tool calls as part of choice.Delta.Content (as JSON strings) or as choice.Delta.FunctionCall (legacy format).
In these cases, the adapter won't process them.
OK, I think the safest fix is to modify the oaistream adapter to also convert FunctionCall to ToolCalls. This way, tool calls in both new format (ToolCalls) and legacy format (FunctionCall) will be processed.
Let me also modify the handleStream function to check both ToolCalls and FunctionCall.
Actually, let me think about this more carefully. The oaistream adapter converts OpenAI stream responses to the cagent format. The handleStream function processes the cagent format.
If the oaistream adapter converts FunctionCall to ToolCalls, the handleStream function will process them correctly.
But there's a potential issue: the FunctionCall doesn't have an ID, but the ToolCall does. The handleStream function expects tool calls to have IDs.
So if LiteLLM returns tool calls in FunctionCall format (without ID), the converted ToolCalls will have empty IDs. This might cause issues in the handleStream function.
Let me check the handleStream function:
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,
})
}
If the ID is empty, the toolCallIndex will use empty string as the key. This could cause issues if there are multiple tool calls without IDs.
But actually, this is fine. Each tool call will have its own entry in the toolCalls slice, with empty ID. The handleStream function will update the tool call's name and arguments.
Wait, but the handleStream function uses delta.ID as the key in toolCallIndex. If multiple tool calls have empty IDs, they'll all use the same key (empty string), and only one will be tracked.
This is a problem. The handleStream function won't properly track multiple tool calls without IDs.
So if LiteLLM returns multiple tool calls in FunctionCall format (without IDs), the handleStream function won't process them correctly.
This is the bug! The fix should ensure that tool calls have IDs even when they're returned in FunctionCall format.
But the oaistream adapter doesn't generate IDs for FunctionCall-based tool calls. So the converted ToolCalls will have empty IDs.
The fix should modify the oaistream adapter to generate IDs for FunctionCall-based tool calls.
Let me implement this fix.
Actually, let me think about this more carefully. The oaistream adapter processes streaming responses. Each chunk may contain a function call delta. The adapter accumulates the function calls across chunks.
Looking at the adapter:
toolCalls: make(map[int]string),
The adapter uses toolCalls map to track tool call IDs by index. When a tool call delta is received, the adapter looks up the index in the map to get the ID. If the ID is not found, it uses the ID from the delta.
For FunctionCall-based tool calls, the adapter sets Delta.FunctionCall but not Delta.ToolCalls. So the handleStream function won't process them.
But wait, the oaistream adapter also has this code:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
This sets Delta.FunctionCall. The handleStream function doesn't check Delta.FunctionCall, so it won't process them.
But there's another issue: LiteLLM might return tool calls in a format that's neither Delta.ToolCalls nor Delta.FunctionCall. For example, LiteLLM might return tool calls as JSON strings in Delta.Content.
In this case, the oaistream adapter will accumulate the JSON strings in Delta.Content. The handleStream function will see them as text content (not as tool calls). Then applyXMLFallback will be called, but it only handles XML tags, not JSON strings.
So if LiteLLM returns tool calls as JSON strings in Delta.Content, the handleStream function won't process them.
OK, I think the safest fix is:
- Modify the oaistream adapter to convert FunctionCall to ToolCalls (with generated IDs)
- Modify the handleStream function to also check Delta.FunctionCall
- Modify
applyXMLFallback (or add a new fallback) to also extract tool calls from JSON strings in the content
- Modify
getLastMessageContentByRole to return tool calls as JSON when text content is empty
Let me implement these fixes.
Actually, this is getting too complicated. Let me step back and think about the simplest fix.
The issue says "Subagent tool_calls not handled when using LiteLLM proxy". The subagent returns a valid tool call, but the host agent says "no response from subagent".
The root cause is that GetLastAssistantMessageContent() returns empty string when the last assistant message has no text content. If the last assistant message has tool calls but no text content, the fix should return a JSON representation of the tool calls.
But if the tool calls weren't accumulated (because LiteLLM returns them in a non-standard format), there are no tool calls to return.
So the fix should be two-fold:
- Ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format
- Modify
GetLastAssistantMessageContent() to return tool calls as JSON when text content is empty
For step 1, the safest approach is to modify the oaistream adapter to also convert FunctionCall to ToolCalls (with generated IDs).
For step 2, the fix is straightforward: modify getLastMessageContentByRole to return tool calls as JSON when text content is empty.
Let me implement both fixes.
Actually, let me first check if LiteLLM returns tool calls in FunctionCall format or in some other format. I can do this by looking at the oaistream adapter and seeing how it handles different formats.
Looking at the adapter, it handles:
- Delta.ToolCalls - standard format
- Delta.FunctionCall - legacy format
- Delta.Content - text content (XML fallback for tool calls)
If LiteLLM returns tool calls in any of these formats, the adapter should handle them (except for FunctionCall, which is not converted to ToolCalls).
So the fix should be to modify the oaistream adapter to convert FunctionCall to ToolCalls.
Let me implement this fix.
Actually, I just realized that the oaistream adapter has this code:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
The adapter sets Delta.FunctionCall. But the handleStream function only checks Delta.ToolCalls:
// Handle tool calls
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
continue
}
So tool calls in FunctionCall format won't be processed.
The fix should modify the oaistream adapter to also convert FunctionCall to ToolCalls.
Let me implement this fix.
OK so I have a clear understanding now. The issue is:
- LiteLLM may return tool calls in FunctionCall format (legacy OpenAI format)
- The oaistream adapter sets Delta.FunctionCall but not Delta.ToolCalls
- The handleStream function only checks Delta.ToolCalls, not Delta.FunctionCall
- Tool calls in FunctionCall format are not processed
- When finish_reason is "stop" and no tool calls are accumulated, the handleStream function returns Stopped=true
- The loop exits without processing tool calls
- The subagent's last assistant message has no content (because tool calls weren't processed)
- GetLastAssistantMessageContent() returns empty string
- The host agent displays "No response from agent"
The fix should:
- Modify the oaistream adapter to convert FunctionCall to ToolCalls
- Modify
getLastMessageContentByRole to return tool calls as JSON when text content is empty
Let me implement both fixes.
Actually, wait. I need to re-read the oaistream adapter more carefully. It has toolCalls map[int]string which tracks tool call IDs by index. This is used for ToolCalls-based tool calls, not FunctionCall-based tool calls.
For FunctionCall-based tool calls, the adapter sets Delta.FunctionCall. But there's no accumulation across chunks for FunctionCall-based tool calls. Each chunk's FunctionCall is a complete function call, not a delta.
So if LiteLLM returns tool calls in FunctionCall format, each chunk may contain a complete function call. The adapter should convert this to ToolCalls.
But the issue is: if multiple chunks contain FunctionCall deltas (for the same or different tool calls), the adapter should accumulate them.
Looking at the adapter:
// Convert function call if present
if choice.Delta.JSON.FunctionCall.Valid() {
funcCall := choice.Delta.FunctionCall
response.Choices[i].Delta.FunctionCall = &tools.FunctionCall{
Name: funcCall.Name,
Arguments: funcCall.Arguments,
}
}
This only sets Delta.FunctionCall. It doesn't accumulate across chunks or convert to ToolCalls.
So if LiteLLM returns tool calls in FunctionCall format, the tool calls won't be accumulated correctly.
The fix should modify the oaistream adapter to:
- Accumulate FunctionCall deltas across chunks (like it does for ToolCalls)
- Convert FunctionCall to ToolCalls
- Generate IDs for FunctionCall-based tool calls
This is getting complicated. Let me think about a simpler fix.
Actually, let me check if LiteLLM returns tool calls in FunctionCall format or in ToolCalls format. LiteLLM is supposed to convert Gemini responses to OpenAI-compatible format. OpenAI uses ToolCalls format for chat completions.
If LiteLLM follows this convention, it should return tool calls in ToolCalls format. So the oaistream adapter should handle them correctly.
But the issue says LiteLLM proxy is used. Maybe LiteLLM returns tool calls in a non-standard format for certain models (like Gemini).
Actually, looking at the issue description:
gemini-pro via LiteLLM proxy (OpenAI-compatible)
So LiteLLM is proxying Gemini responses to OpenAI format. Gemini's tool calling format might be different from OpenAI's format. LiteLLM should convert it to OpenAI format.
If LiteLLM doesn't convert it correctly, the tool calls might be in a non-standard format.
But this is a LiteLLM issue, not a cagent issue. Unless cagent should be more tolerant of non-standard LiteLLM responses.
OK, I think the safest fix is to:
- Modify the oaistream adapter to also convert FunctionCall to ToolCalls (with generated IDs)
- Modify the handleStream function to also check Delta.FunctionCall
- Modify
getLastMessageContentByRole to return tool calls as JSON when text content is empty
Let me implement these fixes.
Actually, I just realized something. Looking at the handleStream function:
// Handle tool calls
if len(choice.Delta.ToolCalls) > 0 {
// Process tool calls
continue
}
This only checks Delta.ToolCalls. If LiteLLM returns tool calls in FunctionCall format, the handleStream function won't process them.
But the handleStream function is in the cagent code, not in the LiteLLM code. So the fix should be in the cagent code to also check Delta.FunctionCall.
Actually, there's another approach. The oaistream adapter converts OpenAI stream responses to cagent format. If the adapter converts FunctionCall to ToolCalls, the handleStream function will process them correctly.
So the fix should be in the oaistream adapter to convert FunctionCall to ToolCalls.
But I also need to handle the case where LiteLLM returns tool calls in a format that the adapter doesn't handle (e.g., as JSON strings in Delta.Content).
OK, I think the simplest fix is:
- Modify the oaistream adapter to convert FunctionCall to ToolCalls (with generated IDs)
- Modify
getLastMessageContentByRole to return tool calls as JSON when text content is empty
Let me implement these fixes.
Actually, I just realized that the issue might not be with LiteLLM returning tool calls in a non-standard format. The issue might be with how the handleStream function handles finish_reason: "stop".
Let me re-read the handleStream function:
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(),
...
Stopped: len(toolCalls) == 0,
...
}, nil
}
When finish_reason is "stop" and there are tool calls:
finishReason is corrected to chat.FinishReasonToolCalls
Stopped is false (because len(toolCalls) > 0)
- Tool calls are returned
So the tool calls should be returned. The loop should process them.
But if the tool calls weren't accumulated (because LiteLLM returns them in a non-standard format), then len(toolCalls) == 0, and:
finishReason remains chat.FinishReasonStop
Stopped is true
- No tool calls are returned
This is the bug. The tool calls weren't accumulated, so the loop exits without processing them.
The fix should ensure that tool calls are accumulated even when LiteLLM returns them in a non-standard format.
For this, I need to modify the oaistream adapter to convert FunctionCall to ToolCalls.
Let me implement this fix.
OK so I've been going in circles. Let me just implement the fix and test it.
The fix should:
- Modify the oaistream adapter to convert FunctionCall to ToolCalls
- Modify
getLastMessageContentByRole to return tool calls as JSON when text content is empty
Let me implement these fixes.
Actually, let me first verify my understanding by checking the existing tests.
<tool_call>
<function=bash_terminal>
<parameter=command>
cd /docker-agent && HOME=/root GOPATH=/root/go GOMODCACHE=/root/go/pkg/mod go test ./pkg/session/... -v 2>&1 | tail -20
</tool_call>