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
Merge branch 'litellm_internal_staging' of https://github.com/BerriAI/litellm into litellm_together_reasoning_effort
mateo-berri9e9c7e621f87
diff --cc litellm/llms/together_ai/chat/transformation.pyindex 2f7a7caecd4,d06bcb22119..47a11230cb1--- a/litellm/llms/together_ai/chat/transformation.py+++ b/litellm/llms/together_ai/chat/transformation.py@@@ -4,8 -4,7 +4,8 @@@ Translates from OpenAI's `/v1/chat/compDocs: https://docs.together.ai/docs/chat-overview"""- from collections.abc import Container, Coroutine, Mapping-from collections.abc import Callable, Container, Coroutine++from collections.abc import Callable, Container, Coroutine, Mapping+from types import MappingProxyTypefrom typing import (Final,Literal,@@@ -19,53 -16,42 +19,70 @@@ import litellfrom litellm._logging import verbose_loggerfrom litellm.exceptions import UnsupportedParamsErrorfrom litellm.types.llms.openai import AllMessageValues- from litellm.utils import supports_function_calling, supports_reasoning-from litellm.utils import supports_function_calling, supports_response_schema++from litellm.utils import supports_function_calling, supports_reasoning, supports_response_schemafrom ...openai.chat.gpt_transformation import OpenAIGPTConfigTOOL_CALLING_PARAMS: Final = ("tools", "tool_choice", "function_call")LITELLM_INTERNAL_ASSISTANT_FIELDS: Final = frozenset({"thinking_blocks", "provider_specific_fields"})- PLAIN_TEXT_RESPONSE_FORMAT: Final = MappingProxyType({"type": "text"})FUNCTION_CALLING_DOCS_URL: Final = "https://docs.together.ai/docs/function-calling"+ STRUCTURED_OUTPUTS_DOCS_URL: Final = "https://docs.together.ai/docs/inference/chat/structured-outputs"+++ def _registry_verdict(model: str, flag: str, check: Callable[[str], bool]) -> bool | None:+ try:+ if check(model):+ return True+ except Exception as e:+ verbose_logger.debug("Error checking together_ai %s for %s: %s", flag, model, e)+ registry_entry: Final = litellm.model_cost.get(f"together_ai/{model}")+ if isinstance(registry_entry, dict) and registry_entry.get(flag) is False:+ return False+ return None++ADJUSTABLE_EFFORT_REASONING_MODELS: Final = frozenset(+ {+ "openai/gpt-oss-120b",+ "openai/gpt-oss-20b",+ }+)+HYBRID_REASONING_MODELS: Final = frozenset(+ {+ "MiniMaxAI/MiniMax-M3",+ "Qwen/Qwen3.5-9B",+ "Qwen/Qwen3.6-Plus",+ "deepseek-ai/DeepSeek-V4-Pro",+ "moonshotai/Kimi-K3",+ "nvidia/nemotron-3-ultra-550b-a55b",+ "zai-org/GLM-5.2",+ }+)+HIGH_MAX_EFFORT_MODEL_PREFIX: Final = "deepseek-ai/DeepSeek-V4-Pro"+EFFORT_TRANSLATION: Final = MappingProxyType({"minimal": "low", "xhigh": "high", "max": "high"})+HIGH_MAX_EFFORT_TRANSLATION: Final = MappingProxyType(+ {"minimal": "high", "low": "high", "medium": "high", "xhigh": "max"}+)+++class TogetherReasoningToggle(TypedDict):+ enabled: ReadOnly[bool]++def _function_calling_verdict(model: str) -> bool | None:- try:- if supports_function_calling(model, custom_llm_provider="together_ai"):- return True- except Exception as e:- verbose_logger.debug("Error checking together_ai function calling support for %s: %s", model, e)- registry_entry: Final = litellm.model_cost.get(f"together_ai/{model}")- if isinstance(registry_entry, dict) and registry_entry.get("supports_function_calling") is False:- return False- return None+ return _registry_verdict(+ model,+ "supports_function_calling",+ lambda checked_model: supports_function_calling(checked_model, custom_llm_provider="together_ai"),+ )+++ def _response_schema_verdict(model: str) -> bool | None:+ return _registry_verdict(+ model,+ "supports_response_schema",+ lambda checked_model: supports_response_schema(checked_model, custom_llm_provider="together_ai"),+ )def _tool_params_to_drop(passed_params: Container[str], model: str, drop_params: bool) -> tuple[str, ...]:@@@ -97,36 -83,32 +114,62 @@@)+def _supports_together_reasoning(model: str) -> bool:+ if model in ADJUSTABLE_EFFORT_REASONING_MODELS or model in HYBRID_REASONING_MODELS:+ return True+ if model.startswith(HIGH_MAX_EFFORT_MODEL_PREFIX):+ return True+ return supports_reasoning(model, custom_llm_provider="together_ai")+++def _adjustable_effort(effort: str, model: str) -> str:+ if effort == "none":+ verbose_logger.debug(+ "together_ai model %s cannot disable reasoning; mapping reasoning_effort=none to low", model+ )+ return "low"+ return EFFORT_TRANSLATION.get(effort, effort)+++def _reasoning_effort_payload(effort: str, model: str) -> Mapping[str, object]:+ if effort == "default":+ return MappingProxyType({})+ if model in ADJUSTABLE_EFFORT_REASONING_MODELS:+ return MappingProxyType({"reasoning_effort": _adjustable_effort(effort, model)})+ if effort == "none":+ disable_reasoning: Final[TogetherReasoningToggle] = {"enabled": False}+ return MappingProxyType({"reasoning": disable_reasoning})+ if model.startswith(HIGH_MAX_EFFORT_MODEL_PREFIX):+ return MappingProxyType({"reasoning_effort": HIGH_MAX_EFFORT_TRANSLATION.get(effort, effort)})+ return MappingProxyType({"reasoning_effort": EFFORT_TRANSLATION.get(effort, effort)})+++ def _drop_response_format(passed_params: Container[str], model: str, drop_params: bool) -> bool:+ if "response_format" not in passed_params:+ return False+ verdict: Final = _response_schema_verdict(model)+ if verdict is True:+ return False+ if verdict is None:+ verbose_logger.warning(+ "together_ai model %s has no structured outputs entry in the model registry; passing response_format through for Together to validate. Docs - %s",+ model,+ STRUCTURED_OUTPUTS_DOCS_URL,+ )+ return False+ if drop_params or litellm.drop_params:+ verbose_logger.warning(+ "together_ai model %s does not support structured outputs per the model registry; dropping response_format. Docs - %s",+ model,+ STRUCTURED_OUTPUTS_DOCS_URL,+ )+ return True+ raise UnsupportedParamsError(+ status_code=500,+ message=f"together_ai does not support parameters: response_format, for model={model}. To drop it from the call, set `litellm.drop_params = True`.",+ )++def _without_litellm_internal_fields(message: AllMessageValues) -> AllMessageValues:if message["role"] != "assistant" or LITELLM_INTERNAL_ASSISTANT_FIELDS.isdisjoint(message):return message@@@ -171,20 -153,6 +214,15 @@@ class TogetherAIChatConfig(OpenAIGPTConreturn super()._transform_messages(stripped, model, is_async=True)return super()._transform_messages(stripped, model, is_async=False)- def get_supported_openai_params(self, model: str) -> list:- supports_fc: Final = _function_calling_verdict(model)++ def get_supported_openai_params(self, model: str) -> list: # mutable-ok: inherited contract+ supported_params: Final = super().get_supported_openai_params(model)- if _supports_together_reasoning(model):- supported_params.append("reasoning_effort")- if supports_fc is True:++ if not _supports_together_reasoning(model):+ return supported_params- verbose_logger.debug(- "Only some together models support response_format. Docs - https://docs.together.ai/docs/function-calling"- )+ return [ # mutable-ok: the inherited contract returns a plain list; building fresh avoids mutating the base class's value- param for param in supported_params if param != "response_format"++ *supported_params,++ "reasoning_effort",+ ]+def map_openai_params(self,non_default_params: dict,@@@ -195,12 -163,6 +233,12 @@@mapped_openai_params: Final = super().map_openai_params(non_default_params, optional_params, model, drop_params)for param in _tool_params_to_drop(mapped_openai_params, model, drop_params):mapped_openai_params.pop(param)- if mapped_openai_params.get("response_format") == PLAIN_TEXT_RESPONSE_FORMAT:+ if _drop_response_format(mapped_openai_params, model, drop_params):mapped_openai_params.pop("response_format")+ effort: Final = mapped_openai_params.get("reasoning_effort")+ if not isinstance(effort, str):+ return mapped_openai_params+ mapped_openai_params.pop("reasoning_effort")+ for key, value in _reasoning_effort_payload(effort, model).items():+ mapped_openai_params.setdefault(key, value)return mapped_openai_paramsdiff --cc litellm/utils.pyindex 59a2de609d7,b164a9c4671..b75d0161cb4--- a/litellm/utils.py+++ b/litellm/utils.py@@@ -2903,10 -3023,15 +3023,14 @@@ def register_modelelif (value.get("cache_creation_input_token_cost") is Noneand value.get("cache_read_input_token_cost") is None+ and value.get("tiered_pricing") is None+ and (- value.get("input_cost_per_token") is not None- or value.get("output_cost_per_token") is not None++ value.get("input_cost_per_token") is not None or value.get("output_cost_per_token") is not None+ )):verbose_logger.warning(- "register_model: model=%s not in built-in cost map and no prefix/region variant matched; cache cost fields will default to 0. To track cache cost, add cache_creation_input_token_cost and cache_read_input_token_cost to model_info",- key,+ "register_model: model=%s has custom pricing but not in built-in cost map and no prefix/region variant matched; cache_creation_input_token_cost and cache_read_input_token_cost will default to 0 for this model (input/output cost tracking is unaffected). To track cache cost, add them to model_info",+ warning_display_name or key,)# ``get_model_info`` returns ``litellm_provider: None`` when the# provider is unknown (e.g. custom deployments registered viadiff --cc tests/test_litellm/llms/together_ai/chat/test_together_ai_chat_transformation.pyindex f85cb6e05dd,bee8ab3a9ac..6b803578067--- a/tests/test_litellm/llms/together_ai/chat/test_together_ai_chat_transformation.py+++ b/tests/test_litellm/llms/together_ai/chat/test_together_ai_chat_transformation.py@@@ -17,29 -18,27 +18,42 @@@ from litellm.types.utils import LlmProvTOOL_CALLING_MODEL = "openai/gpt-oss-20b"REASONING_MODEL = "deepseek-ai/DeepSeek-V3.1"+PLAIN_MODEL = "Qwen/Qwen3-235B-A22B-fp8-tput"UNMAPPED_MODEL = "example-org/brand-new-model"NO_TOOLS_MODEL = "example-org/no-tools-model"+ADJUSTABLE_REASONING_MODEL = "openai/gpt-oss-120b"+HYBRID_REASONING_MODEL = "Qwen/Qwen3.5-9B"+HIGH_MAX_REASONING_MODEL = "deepseek-ai/DeepSeek-V4-Pro"+REGISTRY_FLAGGED_REASONING_MODEL = "zai-org/GLM-4.6"+NON_REASONING_MODEL = "meta-llama/Llama-3.3-70B-Instruct-Turbo"+ NO_SCHEMA_MODEL = "example-org/no-schema-model"TOOL_PARAMS = ("tools", "tool_choice", "function_call")WEATHER_TOOLS = [{"type": "function", "function": {"name": "get_weather", "parameters": {}}}]+ VOICE_NOTE_SCHEMA = {+ "type": "object",+ "properties": {"title": {"type": "string"}, "summary": {"type": "string"}},+ "required": ["title", "summary"],+ "additionalProperties": False,+ }+ JSON_SCHEMA_RESPONSE_FORMAT = {+ "type": "json_schema",+ "json_schema": {"name": "voice_note", "schema": VOICE_NOTE_SCHEMA, "strict": True},+ }+ REGEX_RESPONSE_FORMAT = {"type": "regex", "pattern": "(positive|neutral|negative)"}++def _map_reasoning_effort(model: str, effort: str) -> dict:+ return TogetherAIChatConfig().map_openai_params(+ non_default_params={"reasoning_effort": effort},+ optional_params={},+ model=model,+ drop_params=False,+ )++@pytest.fixture(autouse=True)def force_local_model_cost(monkeypatch):monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")@@@ -182,116 -191,46 +206,156 @@@ def test_map_openai_params_schema_modelassert mapped["response_format"] == response_format+ "model",+ [ADJUSTABLE_REASONING_MODEL, HYBRID_REASONING_MODEL, HIGH_MAX_REASONING_MODEL, REGISTRY_FLAGGED_REASONING_MODEL],+)+def test_supported_params_includes_reasoning_effort_for_reasoning_models(model):+ supported = TogetherAIChatConfig().get_supported_openai_params(model=model)++ assert "reasoning_effort" in supported++[email protected]("model", [NON_REASONING_MODEL, PLAIN_MODEL])+def test_supported_params_excludes_reasoning_effort_for_non_reasoning_models(model):+ supported = TogetherAIChatConfig().get_supported_openai_params(model=model)++ assert "reasoning_effort" not in supported+++ "effort, expected",+ [("low", "low"), ("medium", "medium"), ("high", "high"), ("minimal", "low"), ("xhigh", "high"), ("max", "high")],+)+def test_adjustable_model_translates_reasoning_effort(effort, expected):+ mapped = _map_reasoning_effort(ADJUSTABLE_REASONING_MODEL, effort)++ assert mapped["reasoning_effort"] == expected+ assert "reasoning" not in mapped+++def test_adjustable_model_cannot_disable_reasoning_so_none_becomes_low():+ mapped = _map_reasoning_effort(ADJUSTABLE_REASONING_MODEL, "none")++ assert mapped["reasoning_effort"] == "low"+ assert "reasoning" not in mapped+++ "effort, expected",+ [("low", "low"), ("medium", "medium"), ("high", "high"), ("minimal", "low"), ("xhigh", "high"), ("max", "high")],+)+def test_hybrid_model_translates_reasoning_effort(effort, expected):+ mapped = _map_reasoning_effort(HYBRID_REASONING_MODEL, effort)++ assert mapped["reasoning_effort"] == expected+ assert "reasoning" not in mapped++[email protected]("model", [HYBRID_REASONING_MODEL, HIGH_MAX_REASONING_MODEL, REGISTRY_FLAGGED_REASONING_MODEL])+def test_reasoning_effort_none_becomes_reasoning_toggle(model):+ mapped = _map_reasoning_effort(model, "none")++ assert mapped["reasoning"] == {"enabled": False}+ assert "reasoning_effort" not in mapped+++def test_reasoning_effort_none_does_not_clobber_user_reasoning():+ mapped = TogetherAIChatConfig().map_openai_params(+ non_default_params={"reasoning_effort": "none"},+ optional_params={"reasoning": {"enabled": True}},+ model=HYBRID_REASONING_MODEL,+ drop_params=False,+ )++ assert mapped["reasoning"] == {"enabled": True}+ assert "reasoning_effort" not in mapped+++ "effort, expected",+ [("minimal", "high"), ("low", "high"), ("medium", "high"), ("high", "high"), ("xhigh", "max"), ("max", "max")],+)+def test_deepseek_v4_pro_remaps_to_high_max(effort, expected):+ mapped = _map_reasoning_effort(HIGH_MAX_REASONING_MODEL, effort)++ assert mapped["reasoning_effort"] == expected+++def test_deepseek_v4_pro_dated_variant_remaps_via_prefix():+ mapped = _map_reasoning_effort(f"{HIGH_MAX_REASONING_MODEL}-0813", "low")++ assert mapped["reasoning_effort"] == "high"++[email protected]("model", [ADJUSTABLE_REASONING_MODEL, HYBRID_REASONING_MODEL, HIGH_MAX_REASONING_MODEL])+def test_reasoning_effort_default_is_dropped(model):+ mapped = _map_reasoning_effort(model, "default")++ assert "reasoning_effort" not in mapped+ assert "reasoning" not in mapped+++def test_get_optional_params_translates_reasoning_effort_for_together():+ optional_params = litellm.get_optional_params(+ model=ADJUSTABLE_REASONING_MODEL,+ custom_llm_provider="together_ai",+ reasoning_effort="max",+ )++ assert optional_params["reasoning_effort"] == "high"+++def test_get_optional_params_rejects_reasoning_effort_for_non_reasoning_together_model():+ with pytest.raises(litellm.UnsupportedParamsError):+ litellm.get_optional_params(+ model=NON_REASONING_MODEL,+ custom_llm_provider="together_ai",+ reasoning_effort="low",+ drop_params=False,+ )+++ @pytest.mark.parametrize("drop_params", [False, True])+ def test_map_openai_params_unmapped_model_passes_response_format_through(drop_params, together_warning_log):+ mapped = TogetherAIChatConfig().map_openai_params(+ non_default_params={"response_format": JSON_SCHEMA_RESPONSE_FORMAT},+ optional_params={},+ model=UNMAPPED_MODEL,+ drop_params=drop_params,+ )++ assert mapped["response_format"] == JSON_SCHEMA_RESPONSE_FORMAT+ assert UNMAPPED_MODEL in together_warning_log.text+ assert "passing response_format through" in together_warning_log.text+++ def test_map_openai_params_no_schema_model_drops_response_format_with_warning(+ registry_disables_response_schema, together_warning_log+ ):+ mapped = TogetherAIChatConfig().map_openai_params(+ non_default_params={"response_format": JSON_SCHEMA_RESPONSE_FORMAT, "temperature": 0.5},+ optional_params={},+ model=NO_SCHEMA_MODEL,+ drop_params=True,+ )++ assert "response_format" not in mapped+ assert mapped["temperature"] == 0.5+ assert NO_SCHEMA_MODEL in together_warning_log.text+ assert "dropping response_format" in together_warning_log.text+++ def test_map_openai_params_no_schema_model_raises_without_drop_params(registry_disables_response_schema):+ with pytest.raises(UnsupportedParamsError, match="response_format"):+ TogetherAIChatConfig().map_openai_params(+ non_default_params={"response_format": JSON_SCHEMA_RESPONSE_FORMAT},+ optional_params={},+ model=NO_SCHEMA_MODEL,+ drop_params=False,+ )++def _transform_response(message: dict) -> ModelResponse:raw_response_json = {"id": "chatcmpl-test",