Skip to content

Python: [Bug]: Gemini: a blocked prompt comes back as an empty response with no finish reason #10212

Closed

Description

@nniiovoo

Observed Behavior

When Gemini blocks the prompt itself (for example for safety), the API returns no candidates and sets prompt_feedback.block_reason. GeminiChatClient turns that into an empty assistant message with finish_reason=None, so it looks like the model just answered with nothing. With streaming, usage is lost too, because usage is only attached to a chunk that has a finish reason.

Expected Behavior

finish_reason should be content_filter, the same as when a candidate stops with SAFETY, and streaming should keep the usage.

Steps to Reproduce

  1. Get a response from Gemini where the prompt is blocked (no candidates, prompt_feedback.block_reason set)
  2. Call get_response with and without stream=True
  3. Look at finish_reason and usage_details

Minimal Reproduction

import asyncio
from unittest.mock import AsyncMock, MagicMock

from agent_framework import Message
from google.genai import types

from agent_framework_gemini import GeminiChatClient

# What Gemini returns when it blocks the prompt itself: no candidates, only prompt_feedback.
blocked = types.GenerateContentResponse.model_validate({
    "promptFeedback": {"blockReason": "SAFETY"},
    "usageMetadata": {"promptTokenCount": 8, "totalTokenCount": 8},
})


async def stream():
    yield blocked


async def main():
    genai = MagicMock()
    genai.aio.models.generate_content = AsyncMock(return_value=blocked)
    genai.aio.models.generate_content_stream = AsyncMock(return_value=stream())
    client = GeminiChatClient(client=genai, model="gemini-2.5-flash")
    messages = [Message(role="user", contents=["..."])]

    r = await client.get_response(messages)
    print("non-streaming:", repr(r.text), r.finish_reason, r.usage_details)
    r = await client.get_response(messages, stream=True).get_final_response()
    print("streaming:    ", repr(r.text), r.finish_reason, r.usage_details)


asyncio.run(main())

Error Messages and Stack Traces

non-streaming: '' None {'input_token_count': 8, 'total_token_count': 8}
streaming:     '' None None

Package Versions

agent-framework-core 1.25.0, agent-framework-gemini 1.0.0b261106 (main at 13956ad), google-genai 2.32.0

Python Version

Python 3.12

Operating System

macOS

Regression

No

Additional Context

_process_generate_response and _process_chunk in _chat_client.py only look at the candidate's finish_reason, and never at prompt_feedback. The same happens when a later automatic-continuation hop is blocked: the SDK keeps the earlier candidate, clears its finish_reason and sets prompt_feedback. A fix could be to return content_filter when prompt_feedback.block_reason is set and the candidate has no finish reason. I have a fix with tests and will open a PR.

AI assistance: I used an AI assistant to help find the cause and write this up. I ran the repro myself.

Acknowledgements

  • I searched existing issues and did not find a duplicate.
  • I personally verified this behavior and the reproduction details are authentic.

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Nov 9, 2026
  2. added
    reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
    on Nov 9, 2026
  3. github-actions commented on Nov 9, 2026

    @github-actions
    Contributor

    Reproduced on main (13956ad) with the reporter's scenario, rebuilt independently with a mocked client: both modes return finish_reason=None, and streaming usage_details=None (run). _process_generate_response and _process_chunk in python/packages/gemini/agent_framework_gemini/_chat_client.py only read the candidate's finish reason, never prompt_feedback.

    Support boundary: unclear. I found no maintained spec or docs setting it, so a maintainer should confirm. Fix PR #10213 is open and not merged or released.

    Generated by Issue Routing for #10212 · copilot · auto · ~19 AIC · ⌖ ~1 AIC · ⊞ 5.2K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

pythonUsage: [Issues, PRs], Target: Pythonready-for-implementationMaintainers agree the problem and direction are ready for implementation.reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflowtriageUsage: [Issues], Target: All issues that still need to be triaged

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions