Output Length field (max_output_tokens parameter) does not affect response, setting it causes empty or missing outputs

未关闭
#89 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
api

调研方向

从 google.genai 对 client_gemini.models.generate_content 的 Python 调用开始,比较有无 max_output_tokens 时 GenerateContentConfig 的行为。使用所示值复现短 prompt 的情况,然后检查返回的 candidate、finish reason 和 usage metadata。完成标准是,限制输出的请求返回文本,而不是空的或缺失的 content.parts 响应。

由索引模型根据 Issue 内容生成。

描述

Description of the bug:

When using the official Python Gemini SDK, setting the max_output_tokens (called “Output Length” in UI) parameter in the GenerateContentConfig object does not work as expected.

If I provide a value for max_output_tokens (e.g., 400 or 1024), the model response is empty or contains no text, and response.candidates[0].content.parts is either empty or missing.

This happens even for very short or simple prompts and regardless of the output length requested.

CODE:

from google.genai import types
from google import genai


response = client_gemini.models.generate_content(
    model=gemini_model,
    contents='hi, is sky blue?',
    config=types.GenerateContentConfig(
        max_output_tokens=MAX_OUTPUT_TOKENS,
        temperature=TEMPERATURE,
    ),
)
print(response)

OUTPUT:

sdk_http_response=HttpResponse(
  headers=<dict len=11>
) candidates=[Candidate(
  content=Content(
    role='model'
  ),
  finish_reason=<FinishReason.MAX_TOKENS: 'MAX_TOKENS'>,
  index=0
)] create_time=None response_id=None model_version='gemini-2.5-pro' prompt_feedback=None usage_metadata=GenerateContentResponseUsageMetadata(
  prompt_token_count=7,
  prompt_tokens_details=[
    ModalityTokenCount(
      modality=<MediaModality.TEXT: 'TEXT'>,
      token_count=7
    ),
  ],
  thoughts_token_count=399,
  total_token_count=406
) automatic_function_calling_history=[] parsed=None

Actual vs expected behavior:

If I leave max_output_tokens unset (i.e., use the default), responses are returned normally.

CODE:

from google.genai import types
from google import genai


response = client_gemini.models.generate_content(
    model=gemini_model,
    contents='hi, is sky blue?',
    config=types.GenerateContentConfig(
        temperature=TEMPERATURE,
    ),
)
print(response)

OUTPUT

sdk_http_response=HttpResponse(
  headers=<dict len=11>
) candidates=[Candidate(
  content=Content(
    parts=[
      Part(
        text="""Yes, on a clear day, the sky is blue.

The reason for this is a fascinating bit of physics! Here's a simple breakdown:

1.  **Sunlight is White Light:** Sunlight may look yellow or white, but it's actually made up of all the colors of the rainbow (red, orange, yellow, green, blue, violet).

2.  **Earth Has an Atmosphere:** Our planet is wrapped in a blanket of gases and tiny particles.

3.  **Light Scatters:** When sunlight enters the atmosphere, it bumps into the tiny gas molecules. This causes the light to scatter in all directions.

4.  **Blue Scatters Best:** Shorter wavelengths of light (blue and violet) are scattered much more easily and widely by these gas molecules than the longer wavelengths (like red and orange). This effect is called **Rayleigh scattering**.

Because this blue light is scattered all over the sky, that's the color we see when we look up.

### So, why isn't the sky violet?

That's a great question! Violet light has an even shorter wavelength and is scattered even more than blue. However, two things are happening:
*   The sun emits less violet light than blue light.
*   Our eyes are much more sensitive to blue light than they are to violet.

The combination of these two factors means our eyes and brain perceive the sky as blue.

### When is the sky NOT blue?

*   **At Sunrise and Sunset:** The sky often looks red, orange, or pink. This is because the sun is low on the horizon, and its light has to travel through much more of the atmosphere to reach you. By the time it gets to your eyes, most of the blue light has been scattered away, leaving the longer, reddish wavelengths to come straight through.
*   **On a Cloudy Day:** The sky looks white or gray. Clouds are made of water droplets, which are much larger than gas molecules. They scatter all colors of light about equally. When all the colors mix back together, we see white."""
      ),
    ],
    role='model'
  ),
  finish_reason=<FinishReason.STOP: 'STOP'>,
  index=0
)] create_time=None response_id=None model_version='gemini-2.5-pro' prompt_feedback=None usage_metadata=GenerateContentResponseUsageMetadata(
  candidates_token_count=435,
  prompt_token_count=7,
  prompt_tokens_details=[
    ModalityTokenCount(
      modality=<MediaModality.TEXT: 'TEXT'>,
      token_count=7
    ),
  ],
  thoughts_token_count=1392,
  total_token_count=1834
) automatic_function_calling_history=[] parsed=None
主要语言
Go
星标
143
派生
68
平均合并
10 小时 30 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google-gemini/api-examples 的其他 Issue

查看 google-gemini/api-examples 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。