Skip to content

Function call in stream mode return empyt text #1982

@AmosZhu

Description

@AmosZhu

Environment details

  • Programming language: python
  • OS: Ubunt 24
  • Language runtime version:
  • Package version: 1.60

gemini-3.0-flash & gemini-3.0-pro

Steps to reproduce

When I set in stream mode with function call it's always return empyt in the chunk.text.
SO here is what I found out so far:

  1. The same code when not function call been triggered, everything is fine, the text return normally.
  2. When the tool been triggered (And actually they have been executed in the code), then I will see finish_reason=<FinishReason.STOP: 'STOP'>, in my response. and the text is empty then.

Here is my code:

def _stream_complete_google(
        self,
        config: dict,
        prompt: str,
        images_data: Optional[list[bytes]],
        tools: Optional[list],
        temperature: float,
        history: Optional[MessageHistory],
        usage: Usage,
    ) -> Iterator[tuple[StreamEvent, Optional[Usage]]]:
        """Stream completion using Google genai SDK with AFC enabled."""
        logger.info(f"[STREAM-GOOGLE] Initializing Google client for model: {config['model']}")
        client = genai.Client(api_key=config["api_key"])

        # Build contents
        if history and len(history) > 0:
            contents = history.to_google_contents(images_data)
        else:
            contents = [prompt]
            if images_data:
                for img in images_data:
                    pil_image = Image.open(io.BytesIO(img))
                    contents.append(pil_image)

        # Build generation config with AFC enabled (default)
        generation_config = types.GenerateContentConfig(
            temperature=temperature,
            thinking_config=types.ThinkingConfig(
                thinking_budget=-1,
                include_thoughts=True,
            ),
            tool_config=types.ToolConfig(
                function_calling_config=types.FunctionCallingConfig(mode=types.FunctionCallingConfigMode.AUTO)
            ),
        )

        if tools:
            tool_functions = []
            for tool_item in tools:
                if callable(tool_item):
                    tool_functions.append(tool_item)
                else:
                    func = get_tool_function(tool_item, api_type=config["api_type"])
                    if func:
                        tool_functions.append(func)
            generation_config.tools = tool_functions
            logger.info(f"[STREAM-GOOGLE] Configured {len(tool_functions)} tools with AFC enabled")

        # Track accumulated text for final response
        accumulated_text = ""
        total_prompt_tokens = 0
        total_completion_tokens = 0

        try:
            logger.info("[STREAM-GOOGLE] Starting generate_content_stream with AFC...")
            chunk_count = 0

            for chunk in client.models.generate_content_stream(
                model=config["model"],
                contents=contents,
                config=generation_config,
            ):
                chunk_count += 1

                # Log full chunk structure for debugging
                logger.info(f"[STREAM-GOOGLE] Chunk #{chunk_count}: type={type(chunk).__name__}")

Here is the output of the chunk, the token has been used but nothing return in the text:

GenerateContentResponse( candidates=[ Candidate( content=Content( parts=[ Part( text='' ), ], role='model' ), finish_reason=<FinishReason.STOP: 'STOP'>, index=0 ), ], model_version='gemini-3-flash-preview', response_id='1jlyaaHKNK7tkdUPkf33yAE', sdk_http_response=HttpResponse( headers=<dict len=10> ), usage_metadata=GenerateContentResponseUsageMetadata( candidates_token_count=29, prompt_token_count=1043, prompt_tokens_details=[ ModalityTokenCount( modality=<MediaModality.TEXT: 'TEXT'>, token_count=1043 ), ], thoughts_token_count=152, total_token_count=1224 ) )

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions