fix: Robust handling of array-based tool responses for Gemini compatibility - #17
Merged
Conversation
ouni3
pushed a commit
to ouni3/AIStudioToAPI
that referenced
this pull request
Aug 20, 2026
…bility (iBUHub#17) * fix: enhance tool response handling for array formats in FormatConverter * fix: improve robustness of tool response handling in FormatConverter
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.
English Version
Problem
Gemini API requires the
functionResponse.responsefield to be a JSON Object (Struct), but many tools (like MCP) return an Array[...]. This mismatch caused a400 INVALID_ARGUMENTerror, especially when the tool response contained multiple items or mixed content types (e.g., text + images).Solution
Updated FormatConverter.js to intelligently handle array-based responses:
Single Item:
Multiple/Mixed Items:
{ result: "..." }object.中文版本
问题
Gemini API 要求
functionResponse.response字段必须是一个 JSON 对象(Struct),但许多工具(如 MCP)返回的是数组[...]。这种格式不匹配导致了400 INVALID_ARGUMENT错误,特别是在工具响应包含多个项目或混合内容类型(如文本+图片)时。解决方案
更新了 FormatConverter.js 以智能处理数组格式的响应:
单项数据 (Single Item):
多项/混合数据 (Multiple/Mixed Items):
{ result: "..." }对象中。