Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update map_finish_reason #7264

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions litellm/litellm_core_utils/core_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,62 @@
Span = Any


def map_finish_reason(
finish_reason: str,
): # openai supports 5 stop sequences - 'stop', 'length', 'function_call', 'content_filter', 'null'
# anthropic mapping
if finish_reason == "stop_sequence":
return "stop"
# cohere mapping - https://docs.cohere.com/reference/generate
elif finish_reason == "COMPLETE":
return "stop"
elif finish_reason == "MAX_TOKENS": # cohere + vertex ai
return "length"
elif finish_reason == "ERROR_TOXIC":
return "content_filter"
elif (
finish_reason == "ERROR"
): # openai currently doesn't support an 'error' finish reason
return "stop"
# huggingface mapping https://huggingface.github.io/text-generation-inference/#/Text%20Generation%20Inference/generate_stream
elif finish_reason == "eos_token" or finish_reason == "stop_sequence":
return "stop"
elif (
finish_reason == "FINISH_REASON_UNSPECIFIED" or finish_reason == "STOP"
): # vertex ai - got from running `print(dir(response_obj.candidates[0].finish_reason))`: ['FINISH_REASON_UNSPECIFIED', 'MAX_TOKENS', 'OTHER', 'RECITATION', 'SAFETY', 'STOP',]
return "stop"
elif finish_reason == "SAFETY" or finish_reason == "RECITATION": # vertex ai
return "content_filter"
elif finish_reason == "STOP": # vertex ai
return "stop"
elif finish_reason == "end_turn" or finish_reason == "stop_sequence": # anthropic
return "stop"
elif finish_reason == "max_tokens": # anthropic
return "length"
elif finish_reason == "tool_use": # anthropic
return "tool_calls"
elif finish_reason == "content_filtered":
return "content_filter"
return finish_reason
def map_finish_reason(finish_reason: str) -> str:
"""
Maps finish reasons from various AI providers (OpenAI, Vertex AI, HuggingFace, Cohere, Anthropic) to a consistent set
of values based on OpenAI's finish reason values.

Provider-specific values:
- OpenAI: 'stop', 'length', 'tool_calls', 'content_filter', 'function_call'
- Vertex AI: 'FINISH_REASON_UNSPECIFIED', 'STOP', 'MAX_TOKENS', 'SAFETY', 'RECITATION', 'LANGUAGE', 'OTHER', 'BLOCKLIST', 'PROHIBITED_CONTENT', 'SPII', 'MALFORMED_FUNCTION_CALL'
- HuggingFace: 'stop_sequence', 'eos_token', 'max_tokens'
- Cohere: 'COMPLETE', 'STOP_SEQUENCE', 'MAX_TOKENS', 'TOOL_CALL', 'ERROR'
- Anthropic: 'end_turn', 'max_tokens', 'stop_sequence', 'tool_use'

Provider-speicific values source:
- openai.types.chat.chat_completion.Choice.model_fields['finish_reason']
- google.generativeai.protos.Candidate.FinishReason.__members__.keys()
- cohere.types.ChatFinishReason
- anthropic.types.Message.model_fields['stop_reason']
"""
# Mapping of provider-specific finish reasons to standardized values
finish_reason_mapping = {
# Normal completion reasons
"stop": "stop",
"complete": "stop",
"eos_token": "stop",
"stop_sequence": "stop",
"end_turn": "stop",
"other": "stop",
"finish_reason_unspecified": "stop",

# Error completions reasons
# Mapping to stop since our set of finish reasons doesn't include error
"error": "stop",
"malformed_function_call": "stop",

# Length-related reasons
"length": "length",
"max_tokens": "length",

# Tool/function call reasons
"tool_calls": "tool_calls",
"tool_use": "tool_calls",
"function_call": "tool_calls",
"tool_call": "tool_calls",

# Content filtering/safety reasons
"content_filter": "content_filter",
"error_toxic": "content_filter",
"safety": "content_filter",
"recitation": "content_filter",
"content_filtered": "content_filter",
"blocklist": "content_filter",
"prohibited_content": "content_filter",
"spii": "content_filter",
}

return finish_reason_mapping.get(finish_reason.lower(), finish_reason)


def remove_index_from_tool_calls(messages, tool_calls):
Expand Down
9 changes: 9 additions & 0 deletions tests/local_testing/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,12 @@ def test_token_counter_with_image_url_with_detail_high():
)
print("tokens", _tokens)
assert _tokens == DEFAULT_IMAGE_TOKEN_COUNT + 7

def test_map_finish_reason():
from litellm.litellm_core_utils.core_helpers import map_finish_reason

assert map_finish_reason("STOP") == "stop"
assert map_finish_reason("RECITATION") == "content_filter"
assert map_finish_reason("tool_use") == "tool_calls"
assert map_finish_reason("reason_not_in_mapping") == "reason_not_in_mapping"
assert map_finish_reason("MALFORMED_FUNCTION_CALL") == "stop"