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 uses to get the changes that need to be submitted. Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files for this issue.
Assistant
This seems to be a repository that may not have Python files directly. Let me check what type of project this is.
Assistant
This is a Go project (cagent). Let me explore the structure more to understand the codebase, particularly looking for subagent and tool_calls handling.
Assistant
Let me look at the agent_delegation.go file since it's likely handling subagent delegation:
Assistant
Now let me search for where the subagent response is processed and where "no response from subagent" might be coming from:
Assistant
Let me search for "no response" more broadly:
Assistant
Let me look at the
GetLastAssistantMessageContentmethod to understand how it extracts the message:Assistant
Let me look at the session.go file where
GetLastAssistantMessageContentis likely defined:Assistant
Let me see more of this function and understand how it handles messages with tool_calls:
Assistant
Now I understand the issue better. The
getLastMessageContentByRolefunction only looks at theContentfield of messages. If an assistant message only hastool_callsand no text content (which is valid), it returns an empty string.Let me look at how messages are structured and how tool_calls are stored:
Assistant
Now I can see the structure. The
Messagestruct has bothContentandToolCallsfields. The issue is that when a subagent returns a message with onlyToolCalls(no text content), theGetLastAssistantMessageContent()function returns an empty string because it only looks at theContentfield.Let me look at how the tool call result is returned in
runForwarding:Assistant
Now I see the issue more clearly. The
runForwardingfunction returnstools.ResultSuccess(s.GetLastAssistantMessageContent())which only returns the text content. But if the assistant message only containstool_calls(which is valid), it returns an empty string.However, looking at the issue description again, it says "the host agent reports 'no response from subagent' and does not execute the tool call". This suggests that the issue is that the tool calls themselves are not being executed by the host agent.
Let me look at how the runtime handles tool calls in the main loop:
Assistant
Let me continue reading the loop.go file to understand how tool calls are processed:
Assistant
Let me continue reading the loop to see how tool calls are processed:
Assistant
Let me continue to see how tool calls are processed and look at
recordAssistantMessage:Assistant
Now I need to understand the issue better. Let me look at how the model response is parsed, specifically focusing on how
tool_callsare extracted from the response. The issue mentions that the subagent returns a validtool_callsresponse but it's not being handled. Let me look at how LiteLLM/OpenAI-compatible API responses are parsed:Assistant
Let me look at the OpenAI schema to understand how responses are parsed:
Assistant
Now let me look at the response stream parsing, which is likely where the issue is:
Assistant
This is for the Responses API. Let me look for the Chat Completions API adapter, since LiteLLM proxies typically use Chat Completions API format:
Assistant
Let me look at the oaistream package that handles the chat completion stream:
Assistant
Now let me look at how the stream is aggregated and tool calls are collected. This is where I need to find where the stream result is assembled:
Assistant
Now I found something important! Looking at the
handleStreamfunction, I can see that it processeschoice.Delta.ToolCallsto accumulate tool calls. The key part is in the tool call handling section (around line 178):goif len(choice.Delta.ToolCalls) > 0 { // Process each tool call delta for _, delta := range choice.Delta.ToolCalls { idx, exists := toolCallIndex[delta.ID] if !exists { idx = len(toolCalls) toolCallIndex[delta.ID] = idx ...The issue is that when a tool call delta comes in without an ID (which can happen with some OpenAI-compatible providers like LiteLLM), the code uses
delta.IDas the map key. Ifdelta.IDis empty, multiple tool calls with empty IDs would all map to index 0, and subsequent tool calls would be merged into the same entry!Let me verify this is the issue by looking at how the adapter handles tool calls:
Assistant
I see! The adapter uses the
Indexfield to track tool calls across chunks. It stores the ID by index, so if an ID comes in an earlier chunk, it can be used for subsequent chunks. However, looking at thehandleStreamfunction instreaming.go, it usesdelta.IDdirectly as the key:goidx, exists := toolCallIndex[delta.ID] if !exists { idx = len(toolCalls) toolCallIndex[delta.ID] = idxIf
delta.IDis empty, all tool calls will map to the same key (empty string), causing only one tool call to be tracked!Let me look for tests to understand the expected behavior better:
Assistant
Now let me look at the tests for streaming to understand the expected behavior:
Assistant
Let me look at the adapter test:
Assistant
Now let me look at how the issue manifests. Looking at the issue description more carefully:
The subagent returns a tool call (e.g.,
company_searchwith JSON arguments) and finishes withfinish_reason: "stop".This is key - when using LiteLLM proxy, the subagent response includes
finish_reason: "stop"but also contains atool_callsentry. The issue is that whenfinish_reasonis "stop", thehandleStreamfunction returns early (around line 132-146):goAssistant
I see - the
runForwardingfunction callsr.RunStream(ctx, s)which runs the full agent loop for the subagent. The loop should execute tool calls before completing.Let me look more carefully at how tool calls are handled in the runtime loop. The issue might be in how the tool call ID is matched when it's empty:
Assistant
Now I see the problem more clearly! Look at this code:
goidx, exists := toolCallIndex[delta.ID] if !exists { idx = len(toolCalls) toolCallIndex[delta.ID] = idx ... }When
delta.IDis empty (which can happen with LiteLLM proxy), the key is an empty string"". If there are multiple tool calls with empty IDs, they all map to the same key, so only the first one gets properly tracked!But wait, the adapter.go tries to handle this by tracking IDs using the
Indexfield:gofor 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 }So the adapter tries to preserve the ID across chunks using the index. But the issue is in
handleStream- it usesdelta.IDas the map key, not the index!Let me check if the tool call index is being passed through correctly:
Assistant
Now I see the structure. The
ToolCallstruct in the tools package doesn't have anIndexfield. Let me check the chat.MessageDelta structure:Assistant
I see - the
MessageDelta.ToolCallsis a slice oftools.ToolCall, which only hasID,Type, andFunctionfields. TheIndexfield from the OpenAI SDK's tool call delta is not being preserved in the intermediate representation!Let me check how the OpenAI SDK defines the tool call:
Assistant
Now I understand the full picture! The adapter.go code at line 105 retrieves
toolCall.Indexfrom the OpenAI SDK, but when it creates thetools.ToolCallobject, it doesn't include this index. So by the time the stream reacheshandleStreaminstreaming.go, the index information is lost, and the code can only usedelta.IDto track tool calls.When LiteLLM proxies an OpenAI-compatible API, it might not include the
idfield in tool call deltas (especially if the underlying model doesn't provide it). This causes multiple tool calls to be treated as the same call when they all have empty IDs.The fix should be to use the index of the tool call in the delta array (or track it using a combination of index and ID) instead of relying solely on the ID.
Let me look at how the tool calls come through in more detail:
Assistant
I can see the adapter stores IDs by index (
a.toolCalls[index]), but then it passes the resolvedidto thetools.ToolCallobject. The problem is inhandleStreamwhich usesdelta.IDas the map key. If the ID is empty (and the adapter couldn't resolve it from a previous chunk), multiple tool calls would collide.The fix should be in
streaming.goto handle the case wheredelta.IDis empty. Instead of using just the ID, we should use a combination of the chunk's tool call position and any ID information.Let me trace through what happens when multiple tool calls come in the same chunk without IDs:
- First chunk arrives with
[ToolCall1, ToolCall2]- both with empty IDs - The adapter passes them through with empty IDs
- In
handleStream:delta.IDis""for ToolCall1toolCallIndex[""]doesn't exist, soidx = 0, andtoolCallIndex[""]= 0
- First chunk arrives with
Assistant
Let me create a test script to reproduce the issue first:
Assistant
I've confirmed the bug. When tool calls have empty IDs, they all map to the same index, causing the second tool call to overwrite the first one. The result is only 1 tool call instead of 2, and the arguments get concatenated incorrectly.
Now let me fix the issue. The solution is to use a composite key that includes the position/index of the tool call within the current delta array. Since each chunk can have multiple tool calls, we need to track them by a combination of the ID and the order they appear.
Looking at the code again, I need to modify
streaming.goto handle tool calls without IDs properly:Assistant
I need to use the correct syntax for the open command.
Assistant
Now I understand the issue clearly. The problem is in the tool call handling logic around line 162. When
delta.IDis empty, all tool calls with empty IDs map to the same key intoolCallIndex, causing them to overwrite each other.The fix needs to handle the case where multiple tool calls arrive in the same chunk (or across chunks) without IDs. The key insight is that within a single chunk, each tool call delta has a distinct position in the array. We should use this position to distinguish between different tool calls that have empty IDs.
Let me implement the fix:
Assistant
Let me create a cleaner fix. The issue is that when
delta.IDis empty, multiple tool calls collide. I'll use the index within theToolCallsarray to disambiguate:Assistant
Wait, I need to think about this more carefully. The issue is that when subsequent chunks come in for the same tool call, we need to match them up. Let me think about how OpenAI-compatible providers stream tool calls:
- First chunk: May contain the tool call ID and name (or just name)
- Subsequent chunks: Contain argument deltas, typically with the same ID
If LiteLLM doesn't include IDs at all, we need a different strategy. Let me check how the adapter tracks this:
Assistant
I see - the adapter uses
toolCall.Indexfrom the OpenAI SDK to track IDs across chunks. If the first chunk provides an ID, it stores it and uses it for subsequent chunks. But if no ID is ever provided, the ID remains empty.The key insight is that
toolCall.Indexis provided by the OpenAI SDK for each tool call delta. This index identifies which tool call slot this delta belongs to (0 for first call, 1 for second call, etc.). The adapter uses this to track IDs, but the index itself is lost when converting totools.ToolCall.However, there's an important assumption: within a single delta array, tool calls appear at consistent indices across chunks. So tool call #0 in the first chunk will be at index 0, tool call #1 at index 1, etc. And in subsequent chunks, deltas for those same calls will also appear at those indices.
Let me verify this assumption by looking at how OpenAI structures streaming responses. But first, let me think about a simpler fix:
The problem is when multiple tool calls in the same chunk have empty IDs. In that case, we need to use the position in the array as a key. For subsequent chunks, if they also have empty IDs, the position should match.
Assistant
I see - the adapter uses
toolCall.Indexto track IDs but doesn't pass this index to the downstream code. The fix should be to use this index as the key when the ID is empty.Let me look at what information is available in the delta that reaches
handleStream:Assistant
The
tools.ToolCallstruct doesn't have anIndexfield. Since the OpenAI SDK provides the index but it's lost in conversion, I need a different approach.Looking at this problem from a practical perspective:
- When streaming tool calls, each chunk may have 0 or more tool call deltas
- Each delta has an
Indexfield from the OpenAI SDK indicating which tool call it belongs to - The adapter uses this index to track IDs, but the index is lost
- When IDs are empty, we need to distinguish between different tool calls
The simplest fix is to use the position within the current delta's ToolCalls array combined with a counter for tool calls seen so far. However, this assumes that subsequent chunks maintain the same ordering.
A more robust approach: when an ID is empty, generate a synthetic unique ID and store it back in the tool call. This way, subsequent chunks that also have empty IDs for the same position will still work because we assign the synthetic ID upfront.
Let me implement a cleaner fix:
Assistant
Wait, let me re-read the issue description more carefully:
The subagent response includes a valid tool call object (type: function, name: company_search, arguments: {...}) Host agent reports there is "no response from subagent" and does not invoke the tool.
The "no response from subagent" message is coming from
GetLastAssistantMessageContent()returning an empty string (because the assistant message has tool_calls but no text content). But that's about the result message, not about whether tool calls are executed.The actual issue might be:
- The subagent stream ends with
finish_reason: "stop"(nottool_calls) - The tool call has an empty ID
- The runtime processes the tool call but... something goes wrong
Let me look at how tool calls are processed after the stream completes:
- The subagent stream ends with
Assistant
Let me examine the tool execution dispatcher to see if empty IDs cause issues:
Assistant
Let me continue analyzing the code...
Assistant
So the dispatcher uses
c.tc.ID(the tool call ID) in multiple places, includingToolCallIDin the chat message. Let me check if empty IDs would cause issues in the session message handling...Assistant
Now I understand the issue clearly. Let me implement the fix. The problem is in
handleStreamwhere tool calls with empty IDs collide in thetoolCallIndexmap.Assistant
Let me now apply the fix to the actual file. I need to be more careful with the implementation:
Assistant
Now I'll apply the fix. The key change is to handle empty IDs by using position-based tracking:
Assistant
I made a mistake - the edit command syntax is different. Let me use the proper sed approach or restore the file first:
Assistant
Let me apply the fix properly using a different approach:
Assistant
I need to add the
toolCallsSeenvariable and update the imports. Let me check and fix that.Assistant
I need to declare the
toolCallsSeenvariable. Let me find where the other tracking variables are declared:Assistant
I also need to reset
toolCallsSeenat the beginning of each tool call chunk processing, and after processing all tool calls in a chunk:Assistant
Now let me verify the file compiles correctly and the logic is sound:
Assistant
Now let me verify the changes I made by reviewing the modified section:
Assistant
I see there's a bug in my fix. The
toolCallsSeenvariable shouldn't be reset per chunk - it needs to track the total number of tool calls across all chunks. Let me reconsider the approach.Actually, thinking about this more carefully, the correct approach is simpler: when we encounter a tool call delta with an empty ID, we should check if the position already exists in
toolCalls, and if not, append it. The key insight is that within a single chunk, all deltas are for distinct tool calls, and across chunks, deltas at the same position are for the same tool call.Let me fix this properly:
Assistant
Now let me implement a cleaner fix:
Assistant
The fix has been applied. Now I need to verify it compiles correctly.
Assistant
The build succeeded. Let me verify the changes look correct:
Assistant
I need to run the tests to verify my fix works correctly. Let me run the existing tests in the runtime package.
Assistant
The tests seem to be running. Let me wait for them to complete and check the results:
Assistant
The tests are passing. Let me now run the full test suite for the runtime package to make sure my changes don't break anything else:
Assistant
There's a test failure. Let me see what test failed and why:
Assistant
There are two test failures. Let me examine the first one related to multiple tool calls, which is likely caused by my changes:
Assistant
Let me look at the stream builder to understand how the test constructs the stream:
Assistant
Let me find the streamBuilder in the test file:
Assistant
Let me look at the test harness to understand how the test constructs streams:
Assistant
The streamBuilder is probably defined in the test file itself or in a separate test helper file. Let me search for it.
Assistant
Let me look at the streamBuilder definition to understand how the AddToolCallName and AddToolCallArguments methods work:
Assistant
I see the issue now. The test
TestResponseAPIMultipleToolCallsbuilds a stream where:- First, it adds
AddToolCallName("call_1", "search")- this creates a response with one tool call with IDcall_1 - Then, it adds
AddToolCallName("call_2", "calculate")- this creates another response with one tool call with IDcall_2
Each chunk has one tool call with a different ID. My fix is designed for when a single chunk has multiple tool calls with empty IDs, not when each chunk has one tool call with different IDs.
The issue with my fix is that I changed the
baseIdxcalculation which might be incorrect. Let me look at my fix again:The problem is my assumption about . When each chunk contains one tool call delta with different IDs, my calculation:
- First, it adds
Assistant
Assistant
I accidentally invoked
git checkoutwithout the file argument. Let me restore the file and then apply a corrected fix.Assistant
I need to rewrite the fix more carefully. The key insight is:
- When
delta.IDis non-empty, we should use the ID as the key and track tool calls by ID. - When
delta.IDis empty, we need to use position-based matching.
The problem with my previous fix was:
- I calculated
idx := baseIdx + deltaIdxat the start - But then for ID-based lookups, I used this
idxwhich was wrong
Let me rewrite the fix to be cleaner:
- If
delta.IDis non-empty, use the ID-based approach (as before) - If
delta.IDis empty, use position-based matching
- When
Assistant
I need to test the fix again to see if it resolves the test failures. Let me run the tests again.
Assistant
The test passes now. Let me run all the runtime tests to see if the other failing test is also fixed.
Assistant
The test is still failing, but this time it's a different test - it seems to be related to compaction and session summary, not the tool calls. Let me check which specific test is failing.
Assistant
I need to run a command to check the failing test. Let me use the bash_terminal tool.
Assistant
Let me check if this test was already failing before my changes by reverting and running it.
Assistant
The test was already failing before my changes. This is a pre-existing failure unrelated to my fix. Let me restore my changes and run the tests again to confirm my fix doesn't introduce any new failures.
Assistant
Good, the
TestDoCompactAfterHookFiresfailure is pre-existing and unrelated to my changes. Let me now verify my fix works by running the tests that are specifically related to tool call handling.Assistant
All tool call and stream-related tests are passing. Let me verify the fix by reviewing the diff and checking edge cases.
Assistant
Now let me verify my understanding of the fix and make sure it handles all edge cases properly. Let me also create a simple test to validate the fix works for the specific issue described.
Assistant
The test confirms that my fix correctly handles the case where multiple tool calls arrive in the same chunk with empty IDs. Both tool calls are now tracked separately instead of being merged into one.
Let me verify the fix is complete and run the full test suite to make sure there are no regressions.
Assistant
The test failure is the pre-existing
TestDoCompactAfterHookFirestest that was already failing before my changes. Let me verify this is the only failing test and confirm my fix is complete.Assistant
Only the pre-existing
TestDoCompactAfterHookFirestest is failing, which is unrelated to my changes. My fix is complete and working correctly.Now let me summarize my changes and verify the fix once more by reviewing the final diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.