The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(types): expose native_finish_reason in provider_specific_fields
Chesarsd501c33a9dc5
diff --git a/docs/my-website/docs/completion/output.md b/docs/my-website/docs/completion/output.mdindex f705bc9f311..a7f26a0ec37 100644--- a/docs/my-website/docs/completion/output.md+++ b/docs/my-website/docs/completion/output.md@@ -51,6 +51,28 @@ Here's what an example response looks like}```+## Native Finish Reason++LiteLLM maps all provider-specific `finish_reason` values to OpenAI-compatible values (`stop`, `length`, `tool_calls`, `function_call`, `content_filter`). When the original provider value differs from the mapped value, it is preserved in `provider_specific_fields["native_finish_reason"]`.++This is useful for agent loops that need to distinguish between different stop conditions (e.g., Gemini's `MALFORMED_FUNCTION_CALL` vs a normal `stop`).++```python+response = completion(model="gemini/gemini-2.0-flash", messages=messages)++choice = response.choices[0]+print(choice.finish_reason) # "stop" (OpenAI-compatible)++# Access the original provider value when it differs:+if hasattr(choice, "provider_specific_fields") and choice.provider_specific_fields:+ native = choice.provider_specific_fields.get("native_finish_reason")+ if native == "MALFORMED_FUNCTION_CALL":+ # Handle malformed function call differently from a normal stop+ pass+```++When the provider already returns an OpenAI-compatible value (e.g., `stop`), `native_finish_reason` is not set.+## Additional AttributesYou can also access information like latency.diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.pyindex 2a737f26938..74d5ad330e5 100644--- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py+++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py@@ -1241,7 +1241,7 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):"""from litellm.litellm_core_utils.core_helpers import _FINISH_REASON_MAP- return _FINISH_REASON_MAP+ return dict(_FINISH_REASON_MAP)def translate_exception_str(self, exception_string: str):if (diff --git a/litellm/types/utils.py b/litellm/types/utils.pyindex b5d5c06924d..50960134695 100644--- a/litellm/types/utils.py+++ b/litellm/types/utils.py@@ -1326,7 +1326,11 @@ class Choices(SafeAttributeModel, OpenAIObject):**params,):if finish_reason is not None:- params["finish_reason"] = map_finish_reason(finish_reason)+ mapped = map_finish_reason(finish_reason)+ params["finish_reason"] = mapped+ if finish_reason != mapped:+ provider_specific_fields = provider_specific_fields or {}+ provider_specific_fields["native_finish_reason"] = finish_reasonelse:params["finish_reason"] = "stop"if index is not None:diff --git a/tests/test_litellm/types/test_types_utils.py b/tests/test_litellm/types/test_types_utils.pyindex 8c20ace98a0..ccd467a1f7b 100644--- a/tests/test_litellm/types/test_types_utils.py+++ b/tests/test_litellm/types/test_types_utils.py@@ -223,3 +223,59 @@ def test_chat_completion_token_logprob_invalid_top_logprobs_rejected():logprob=-0.31725305,top_logprobs="invalid_string",)+++# ---------------------------------------------------------------------------+# native_finish_reason in provider_specific_fields+# ---------------------------------------------------------------------------+++class TestNativeFinishReason:+ """Choices exposes the raw provider finish_reason in provider_specific_fields+ when it differs from the mapped OpenAI-compatible value."""++ def test_provider_reason_exposed_when_mapped(self):+ from litellm.types.utils import Choices++ choice = Choices(finish_reason="end_turn")+ assert choice.finish_reason == "stop"+ assert choice.provider_specific_fields["native_finish_reason"] == "end_turn"++ def test_provider_reason_not_set_when_already_openai(self):+ from litellm.types.utils import Choices++ choice = Choices(finish_reason="stop")+ assert choice.finish_reason == "stop"+ assert not hasattr(choice, "provider_specific_fields")++ def test_provider_reason_merged_with_existing_fields(self):+ from litellm.types.utils import Choices++ choice = Choices(+ finish_reason="max_tokens",+ provider_specific_fields={"citations": [{"url": "http://example.com"}]},+ )+ assert choice.finish_reason == "length"+ assert choice.provider_specific_fields["native_finish_reason"] == "max_tokens"+ assert choice.provider_specific_fields["citations"] == [{"url": "http://example.com"}]++ def test_gemini_safety_reason_exposed(self):+ from litellm.types.utils import Choices++ choice = Choices(finish_reason="SAFETY")+ assert choice.finish_reason == "content_filter"+ assert choice.provider_specific_fields["native_finish_reason"] == "SAFETY"++ def test_anthropic_tool_use_reason_exposed(self):+ from litellm.types.utils import Choices++ choice = Choices(finish_reason="tool_use")+ assert choice.finish_reason == "tool_calls"+ assert choice.provider_specific_fields["native_finish_reason"] == "tool_use"++ def test_max_tokens_reason_exposed(self):+ from litellm.types.utils import Choices++ choice = Choices(finish_reason="MAX_TOKENS")+ assert choice.finish_reason == "length"+ assert choice.provider_specific_fields["native_finish_reason"] == "MAX_TOKENS"