#26272·mlflow

[BUG] AI 网关 Anthropic 和 Gemini 提供程序在工具调用时报告 finish_reason 为 "stop" 而不是 "tool_calls"

作者: r14dd创建于 2026年9月29日更新于 2026年9月30日
标签area/gateway

When a turn ends in a tool call, the Anthropic and Gemini gateway providers report `finish_reason="stop"`. The OpenAI chat format the gateway targets reports `"tool_calls"`, and clients that decide whether to run tools by looking at `finish_reason` will treat the turn as a finished text answer. - **Anthropic:** `stop_reason: "tool_use"` becomes `"stop"`. The mapping is `"length" if stop_reason == "max_tokens" else "stop"` in `model_to_chat` (`Anthropic.py:372`) and `model_to_chat_streaming` (`Anthropic.py:452`), so every other reason falls to `"stop"`. - **Gemini:** `_normalize_finish_reason` (`Gemini.py:100-106`) only maps `MAX_TOKENS` and lowercases the rest. A response with a `functionCall` part comes back with `finishReason: "STOP"`, so it is reported as `"stop"`. This applies to `model_to_chat` and `model_to_chat_streaming`, and so to the Vertex adapter that inherits them. In both cases `tool_calls` is populated correctly. Only `finish_reason` is wrong. Expected: `"tool_calls"` when the choice carries tool calls, on all four paths (both providers, streaming and non-streaming).