#10662·agno

[Bug] Gemini 3.x: ToolResult media still causes 400 "Requests ending with a model turn are not supported" — #9647's nesting never runs in the agent loop

Author: RaphalRiadCreated Sep 29, 2026Updated Sep 30, 2026

Description

When a tool returns media (ToolResult(images=...), files=, audios=, videos=), the next Gemini 3.x call fails with 400 INVALID_ARGUMENT: Requests ending with a model turn are not supported.

#9630 reported this, and #9647 closed it by nesting supported media in FunctionResponse.parts in gemini.py. That path never runs in a real agent run. Model._handle_function_call_media in models/base.py runs first: it removes the media from the tool message and appends a separate role="user" message with the text "The tool call above generated the attached media." plus the media. Gemini._format_messages then merges that message into the tool-result turn. The request becomes user: [functionResponse (0 media parts), text, inline_data], which is the shape #9630 reported.

#9647's unit tests call _format_messages directly, with the media still on the tool message, so they pass while the full loop fails.

Steps to Reproduce

python
import asyncio, struct, zlib
from agno.agent import Agent
from agno.models.google import Gemini
from agno.media import Image
from agno.tools.function import ToolResult

def png(w=64, h=64, rgb=(220, 20, 20)):
    row = b"\x00" + bytes(rgb) * w
    chunk = lambda t, d: struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d))
    return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0))
            + chunk(b"IDAT", zlib.compress(row * h)) + chunk(b"IEND", b""))

def view(document_id: str) -> ToolResult:
    """Open a document."""
    return ToolResult(content="swatch.png", images=[Image(content=png(), mime_type="image/png")])

async def main():
    agent = Agent(model=Gemini(id="gemini-3.6-flash", vertexai=True), tools=[view])
    async for ev in agent.arun("Open document d1 and tell me its colour.", stream=True, stream_events=True):
        pass

asyncio.run(main())

Expected Behavior

The image is sent nested in the functionResponse (as #9647 intends), and the model answers "Red".

Actual Behavior

The request after the tool call is user: [functionResponse, text, inline_data], and Gemini returns a 400. Every media type fails the same way: PNG, JPEG, WebP, PDF, WAV, MP3, OGG and MP4.

Screenshots or Logs (if applicable)

ERROR Error from Gemini API: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Requests ending with a model turn are not supported.', 'status': 'INVALID_ARGUMENT'}}

Environment

  • agno 3.0.10. _handle_function_call_media is unchanged in 3.0.11.
  • gemini-3.6-flash on Vertex AI (location global).
  • Python 3.13.

Possible Solutions (optional)

We work around it with a small Gemini subclass, verified live on all of the media types above:

  1. Override _handle_function_call_media to leave the media on the tool message, so #9647's nesting applies.
  2. After super()._format_messages(), split any user Content that has function_response parts plus other parts into two consecutive user Contents. This covers media Gemini can't nest (audio, video) and #10455's case. Vertex accepts consecutive user contents.

A proper upstream fix would let a model opt out of the split in base.py (as #7661 asks) and add a test that drives the full agent loop instead of _format_messages alone.

Additional Context

Related: #9630 (closed by #9647), #7661 (open, same root cause), #10455 (open, the same merge problem after stop_after_tool_call).