Skip to content

Gemini streaming error: "Invalid character '{' after top-level value" #149

@djoreilly

Description

@djoreilly

Get this error sometimes when using Gemini and streaming is enabled:

agent error: convert schema message failed: unmarshal tool call arguments failed: invalid character '{' after top-level value

Reproducer:

from typing import List, Dict, Any
from fastmcp import FastMCP, Context

mcp = FastMCP("fruit-store")


@mcp.tool()
def list_fruits(ctx: Context) -> List[str]:
    """
    Returns all fruits for sale in the store.
    """
    return ["apples", "oranges"]


@mcp.tool()
def fruit_details(ctx: Context, fruit_name: str) -> Dict[str, Any]:
    """Gets details of the specified fruit

    Args:
        fruit_name: the name of the fruit.
    Returns:
        An object that contains the following:
            - fruit_name: the name of the fruit
            - price: the price of the fruit
    """
    return {"fruit_name": fruit_name, "price": 42}
{
  "mcpServers": {
    "fruit-store": {
      "command": "uv",
      "args": ["run", "fastmcp", "run", "main.py:mcp"]
    }
  }
}
$ mcphost -m google:gemini-2.5-flash --config mcphost-fruit.json
  ┃  what fruit is the cheapest?                                                                              ┃
  ┃                                                                                         doreilly (10:28)  ┃
  ┃                                                                                                           ┃

  ┃                                               ┃
  ┃   Executing fruit-store__list_fruits (10:28)  ┃
  ┃                                               ┃

  ┃                                                                                                           ┃
  ┃  ["apples","oranges"]                                                                                     ┃
  ┃                                                                                                           ┃

  ┃                                                            ┃
  ┃  Arguments: "fruit_name":"apples"}{"fruit_name":"oranges"  ┃
  ┃   Executing fruit-store__fruit_details (10:28)             ┃
  ┃                                                            ┃

  ┃                                                                                                    ┃
  ┃  Error: Tool execution error: invalid JSON arguments: invalid character '{' after top-level value  ┃
  ┃                                                                                                    ┃

  ┃
  ┃
  ┃  agent error: convert schema message failed: unmarshal tool call arguments failed: invalid character '{' after top-level value  ┃
  ┃   Error (10:28)
  ┃
  ┃
  ┃

  ┃
  ┃
  ┃  agent error: convert schema message failed: unmarshal tool call arguments failed: invalid character '{' after top-level value  ┃
  ┃   Error (10:28)

The error does not happen every time, and it never happens if --stream=false is used.
I added lines to print the msgs that are coming from Gemini here. When the problem happens, the msg looks like this:

{
  "role": "assistant",
  "content": "",
  "tool_calls": [
    {
      "id": "fruit-store__fruit_details",
      "type": "",
      "function": {
        "name": "fruit-store__fruit_details",
        "arguments": "{\"fruit_name\":\"apples\"}"
      }
    },
    {
      "id": "fruit-store__fruit_details",
      "type": "",
      "function": {
        "name": "fruit-store__fruit_details",
        "arguments": "{\"fruit_name\":\"oranges\"}"
      }
    }
  ],
  "response_meta": {
    "finish_reason": "STOP",
    "usage": {
      "prompt_tokens": 202,
      "prompt_token_details": {
        "cached_tokens": 0
      },
      "completion_tokens": 43,
      "total_tokens": 245,
      "completion_token_details": {}
    }
  }
}

It seems Gemini decided to use parallel function calling in this case.
The problem for mcphost is because Gemini uses the same "id": "fruit-store__fruit_details" for both tool calls, and this is used as the key in the accumulatedToolCalls map.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions