[BUG] AI Gateway Anthropic and Gemini providers report finish_reason "stop" instead of "tool_calls" on tool calls
[!WARNING] Before submitting a PR, please make sure that:
- A maintainer has triaged this issue and applied the
readylabel- This issue has no assignee
- No duplicate PR exists
PRs not meeting these requirements may be automatically closed.
Issues Policy acknowledgement
- I have read and agree to submit bug reports in accordance with the issues policy
Where did you encounter this bug?
Local machine
MLflow version
- Client: 3.16.2.dev0 (master,
20aee4415).
System information
- macOS 15, Python 3.10.21
Describe the problem
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"inmodel_to_chat(anthropic.py:372) andmodel_to_chat_streaming(anthropic.py:452), so every other reason falls to"stop". - Gemini:
_normalize_finish_reason(gemini.py:100-106) only mapsMAX_TOKENSand lowercases the rest. A response with afunctionCallpart comes back withfinishReason: "STOP", so it is reported as"stop". This applies tomodel_to_chatandmodel_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).
Tracking information
N/A, gateway only.
Code to reproduce issue
Feeds each adapter a response that ends in a tool call, no network. Run from the repo root with python repro.py.
import sys
sys.path.insert(0, ".")
from mlflow.gateway.config import EndpointConfig
from mlflow.gateway.providers.anthropic import AnthropicAdapter
from mlflow.gateway.providers.gemini import GeminiAdapter
def config(provider, name, key):
return EndpointConfig(name="chat", endpoint_type="llm/v1/chat",
model={"provider": provider, "name": name, "config": {key: "key"}})
anthropic_cfg = config("anthropic", "claude-sonnet-5", "anthropic_api_key")
gemini_cfg = config("gemini", "gemini-2.0-flash", "gemini_api_key")
anthropic_resp = {
"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-sonnet-5",
"content": [{"type": "tool_use", "id": "toolu_A", "name": "get_weather", "input": {"city": "Paris"}}],
"stop_reason": "tool_use", "stop_sequence": None,
"usage": {"input_tokens": 30, "output_tokens": 40},
}
print(AnthropicAdapter.model_to_chat(anthropic_resp, anthropic_cfg).choices[0].finish_reason)
print(AnthropicAdapter.model_to_chat_streaming(
{"type": "message_delta", "id": "msg_1", "model": "claude-sonnet-5", "index": 0,
"delta": {"stop_reason": "tool_use", "stop_sequence": None}}, anthropic_cfg,
).choices[0].finish_reason)
gemini_resp = {
"candidates": [{"content": {"role": "model", "parts": [
{"functionCall": {"name": "get_weather", "args": {"city": "Paris"}}}]},
"finishReason": "STOP", "index": 0}],
"usageMetadata": {"promptTokenCount": 30, "candidatesTokenCount": 10, "totalTokenCount": 40},
"modelVersion": "gemini-2.0-flash",
}
print(GeminiAdapter.model_to_chat(gemini_resp, gemini_cfg).choices[0].finish_reason)
print(GeminiAdapter.model_to_chat_streaming(gemini_resp, gemini_cfg).choices[0].finish_reason)
# stop, stop, stop, stopStack trace
No exception. The client just sees a text-style finish.
Other info / logs
Proposed fix: map Anthropic tool_use to "tool_calls" in both methods. For Gemini, set "tool_calls" when the candidate has a functionCall part, since Gemini reports STOP for those. Tests: one per provider and path with a tool call, asserting finish_reason == "tool_calls".
This touches lines near the streaming index fixes in #26269 and #26270, so I would open it after those land to avoid conflicts. Happy to take it if assigned.
Willingness to contribute
Yes. I can contribute a fix for this bug independently.
What component(s) does this bug affect?
-
area/gateway: MLflow AI Gateway client APIs, server, and third-party integrations
Source: mlflow/mlflow