fix(adk): fix ToolCall Arguments format in GenTransferMessages#741
Open
shenysun wants to merge 16 commits intocloudwego:alpha/08from
Open
fix(adk): fix ToolCall Arguments format in GenTransferMessages#741shenysun wants to merge 16 commits intocloudwego:alpha/08from
shenysun wants to merge 16 commits intocloudwego:alpha/08from
Conversation
| tooCall := schema.ToolCall{ID: toolCallID, Function: schema.FunctionCall{Name: TransferToAgentToolName, Arguments: destAgentName}} | ||
| args := map[string]string{"agent_name": destAgentName} | ||
| argsJSON, _ := json.Marshal(args) | ||
| tooCall := schema.ToolCall{ID: toolCallID, Function: schema.FunctionCall{Name: TransferToAgentToolName, Arguments: string(argsJSON)}} |
Author
There was a problem hiding this comment.
- 我们的 LLM API 基于 OpenAI 协议封装,对 tool_calls 字段有严格校验。多轮对话中传递历史消息时,若格式不符合规范会报错。
- 在 OpenAI 协议中,tool_calls arguments 定义为 JSON-encoded 格式。
https://platform.openai.com/docs/guides/function-calling?api-mode=chat#handling-function-calls
Contributor
There was a problem hiding this comment.
openai 模型并不会因为这个 argument 不是 json 而报错
Author
There was a problem hiding this comment.
确实,OpenAI 模型本身不会因为这个报错。这个问题是我们内部 LLM API 封装层的校验逻辑导致的,不是框架的 bug。
不过从协议规范角度,tool_calls.arguments 确实应该是 JSON-encoded 格式。我的修改让代码更符合 OpenAI 官方规范。
Contributor
There was a problem hiding this comment.
"JSON-encoded arguments",这个的字面意思不是一定是 object。简单的一个 string 也是 json-encoded.
Author
There was a problem hiding this comment.
您说的没毛病,从 JSON 规范角度 "hello" 确实是 JSON-encoded。
但我的修改考虑的是:
- 工具定义的对应关系:
transfer_to_agent工具在代码中明确定义了参数是对象类型
// adk/chatmodel.go:417-427
toolInfoTransferToAgent = &schema.ToolInfo{
Name: TransferToAgentToolName,
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"agent_name": {
Desc: "the name of the agent to transfer to",
Required: true,
Type: schema.String,
},
}),
}工具的参数定义是一个包含 agent_name 字段的对象,而不是简单字符串。
- 解析代码的期望(目前这个场景不会解析,deterministic_transfer 直接构造的tool msg):
transfer_to_agent的实现使用UnmarshalString解析参数
// adk/chatmodel.go:495-500
type transferParams struct {
AgentName string `json:"agent_name"`
}
params := &transferParams{}
err := sonic.UnmarshalString(argumentsInJSON, params)这段代码期望 argumentsInJSON 是一个 JSON 对象字符串 {"agent_name": "xxx"},才能正确解析到 params.AgentName。
…loudwego#722) * feat: add enhanced tool support with multimodal output capabilities and improve message formatting This commit introduces enhanced tool interfaces that support structured multimodal outputs, enabling tools to return rich content beyond simple text responses. Key Changes: 1. New Enhanced Tool Interfaces: - Added EnhancedInvokableTool and EnhancedStreamableTool interfaces for multimodal tool execution - Both interfaces use ToolCallInfo as input and return ToolResult for structured output 2. ToolResult Schema: - Introduced ToolResult type to represent multimodal tool outputs - Supports multiple content types: text, image, audio, video, and file - Added ToolOutputPart with Index field for streaming chunk merging - Implemented ToMessageInputParts() for seamless model integration 3. ToolsNode Enhancements: - Extended ToolsNode to support both legacy and enhanced tool types - Added automatic conversion between invokable and streamable endpoints - Implemented middleware support for enhanced tools - Enhanced interrupt and rerun mechanism to handle ToolResult 4. React Agent Integration: - Introduce enhancedToolResultSender and enhancedStreamToolResultSender types - Support sending *schema.ToolResult with multimodal content (images, audio, video, files) - Implement EnhancedInvokable and EnhancedStreamable middleware in tool result collector 5. Message.String() Enhancement: - Add formatting support for UserInputMultiContent, AssistantGenMultiContent, and MultiContent - Implement formatInputPart, formatOutputPart, and formatChatMessagePart helper functions - Create mediaPartFormatter interface with wrapper types for unified media formatting 6. User Input Multi-Content Concatenation: - Implement concatUserMultiContent function for merging MessageInputPart slices - Support text and base64 audio merging with proper MIME type handling - Integrate into ConcatMessages function 7. Callback System: - Added CallbackInput and CallbackOutput types for tool callbacks - Implemented conversion functions for different callback input/output types 8. Comprehensive Test Coverage: - Added tests for enhanced invokable and streamable tools - Added TestMessageString with 14 test cases covering various message types Impact: - Enables tools to return rich multimodal content (images, audio, video, files) - Provides foundation for more sophisticated tool implementations - Maintains full backward compatibility with existing tool ecosystem
…string]bool (cloudwego#737) - Update ToolsConfig.ReturnDirectly type - Update ChatModelAgentContext.ReturnDirectly type - Update internal types: chatModelAgentExecCtx, execContext, reactConfig - Update all related function signatures - Update test files with new type syntax Change-Id: I7d819f1c44da91b76cf9a9f867a88008068153b8
…AfterModelRewriteState (cloudwego#745)
8b3de42 to
b1fbde2
Compare
…nd reduction packages (cloudwego#749)
Change-Id: I61cd3709e78b5e1ef1fe169572913e7e35946d56
Update test assertions to expect JSON-formatted arguments for transfer_to_agent tool calls, matching the fixed format from GenTransferMessages.
fc34517 to
4186c88
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
fix
Check the PR title.
(Optional) Translate the PR title into Chinese.
修复 GenTransferMessages 中的 ToolCall Arguments 格式问题
(Optional) More detailed description for this PR(en: English/zh: Chinese).
This PR fixes the ToolCall Arguments format in the
GenTransferMessagesfunction. Previously, the Arguments field directly used the destination agent name as a plain string, which doesn't conform to the standard JSON object format for tool calls. This would cause tool call parsing to fail in supervisor scenarios when transferring between agents.The fix changes the Arguments from:
To the proper JSON object format:
Files Changed:
adk/utils.go: Added JSON marshaling for tool call argumentsadk/utils_test.go: Added comprehensive unit tests for GenTransferMessagesTesting: