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
fix(fireworks_ai): prefer native values silently on extras conflicts
Miles Adkins431f61b4f7c2
diff --git a/litellm/llms/fireworks_ai/chat/transformation.py b/litellm/llms/fireworks_ai/chat/transformation.pyindex b5c82129f45..3bacb3cd28e 100644--- a/litellm/llms/fireworks_ai/chat/transformation.py+++ b/litellm/llms/fireworks_ai/chat/transformation.py@@ -311,7 +311,6 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):if not isinstance(extra_body, dict):return dict(optional_params) # mutable-ok: JSON request body- self._validate_extra_body_conflicts(extra_body=extra_body, optional_params=optional_params, model=model)stripped: Final = tuple(sorted(k for k in extra_body if k in _NIM_VLLM_STRIP_PARAMS))if stripped:verbose_logger.debug(@@ -320,9 +319,9 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):model,)promoted: Final = (- *self._translate_truncate_prompt_tokens(extra_body),- *self._translate_chat_template_kwargs(extra_body, model),- *self._translate_guided_params(extra_body),+ *self._translate_truncate_prompt_tokens(extra_body, optional_params),+ *self._translate_chat_template_kwargs(extra_body, optional_params, model),+ *self._translate_guided_params(extra_body, optional_params),)remaining: Final = tuple((k, v) for k, v in extra_body.items() if k not in _EXTRA_BODY_CONSUMED_PARAMS)base: Final = {k: v for k, v in optional_params.items() if k != "extra_body"} # mutable-ok: JSON request body@@ -332,74 +331,32 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):**({"extra_body": dict(remaining)} if remaining else {}), # mutable-ok: JSON request body}- def _validate_extra_body_conflicts(- self, extra_body: Mapping[str, object], optional_params: Mapping[str, object], model: str- ) -> None:- if "truncate_prompt_tokens" in extra_body and (- "prompt_truncate_len" in extra_body or "prompt_truncate_len" in optional_params- ):- raise litellm.BadRequestError(- message=(- "Fireworks AI chat completions received both `truncate_prompt_tokens` and "- "`prompt_truncate_len`; they are aliases, send only one."- ),- model=model,- llm_provider="fireworks_ai",- )- chat_template_kwargs: Final = extra_body.get("chat_template_kwargs")- if (- isinstance(chat_template_kwargs, dict)- and "enable_thinking" in chat_template_kwargs- and ("reasoning_effort" in optional_params or "thinking" in optional_params)- ):- raise litellm.BadRequestError(- message=(- "Fireworks AI chat completions does not support specifying both "- "`chat_template_kwargs.enable_thinking` and `reasoning_effort`/`thinking` in the same request."- ),- model=model,- llm_provider="fireworks_ai",- )- guided_params: Final = tuple(- k for k in ("guided_json", "guided_grammar", "guided_choice") if extra_body.get(k) is not None- )- if len(guided_params) > 1:- raise litellm.BadRequestError(- message=(- f"Fireworks AI chat completions received multiple guided decoding params "- f"{guided_params}; send only one."- ),- model=model,- llm_provider="fireworks_ai",- )- if guided_params and "response_format" in optional_params:- raise litellm.BadRequestError(- message=(- f"Fireworks AI chat completions received both `{guided_params[0]}` and "- "`response_format`; they are competing output constraints, send only one."- ),- model=model,- llm_provider="fireworks_ai",- )-@staticmethod- def _translate_truncate_prompt_tokens(extra_body: Mapping[str, object]) -> tuple[tuple[str, object], ...]:+ def _translate_truncate_prompt_tokens(+ extra_body: Mapping[str, object], optional_params: Mapping[str, object]+ ) -> tuple[tuple[str, object], ...]:if extra_body.get("truncate_prompt_tokens") is None:return ()+ if "prompt_truncate_len" in extra_body or "prompt_truncate_len" in optional_params:+ verbose_logger.debug(+ "fireworks_ai ignoring truncate_prompt_tokens; explicit prompt_truncate_len takes precedence."+ )+ return ()return (("prompt_truncate_len", extra_body["truncate_prompt_tokens"]),)def _translate_chat_template_kwargs(- self, extra_body: Mapping[str, object], model: str+ self, extra_body: Mapping[str, object], optional_params: Mapping[str, object], model: str) -> tuple[tuple[str, object], ...]:chat_template_kwargs: Final = extra_body.get("chat_template_kwargs")if chat_template_kwargs is None:return ()if not isinstance(chat_template_kwargs, dict):- raise litellm.BadRequestError(- message="Fireworks AI chat completions expects `chat_template_kwargs` to be an object.",- model=model,- llm_provider="fireworks_ai",+ verbose_logger.debug(+ "fireworks_ai dropping chat_template_kwargs for model=%s; expected an object, got %s.",+ model,+ type(chat_template_kwargs).__name__,)+ return ()other_keys: Final = tuple(sorted(k for k in chat_template_kwargs if k != "enable_thinking"))if other_keys:verbose_logger.debug(@@ -409,6 +366,11 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):)if "enable_thinking" not in chat_template_kwargs:return ()+ if "reasoning_effort" in optional_params or "thinking" in optional_params:+ verbose_logger.debug(+ "fireworks_ai ignoring chat_template_kwargs.enable_thinking; explicit reasoning_effort/thinking takes precedence."+ )+ return ()if not supports_reasoning(model=model, custom_llm_provider="fireworks_ai"):verbose_logger.debug("fireworks_ai model %r does not support reasoning; dropping chat_template_kwargs.enable_thinking.",@@ -420,7 +382,19 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):return (("reasoning_effort", "none"),)@staticmethod- def _translate_guided_params(extra_body: Mapping[str, object]) -> tuple[tuple[str, object], ...]:+ def _translate_guided_params(+ extra_body: Mapping[str, object], optional_params: Mapping[str, object]+ ) -> tuple[tuple[str, object], ...]:+ has_guided: Final = any(+ extra_body.get(key) is not None for key in ("guided_json", "guided_grammar", "guided_choice")+ )+ if not has_guided:+ return ()+ if "response_format" in optional_params or "response_format" in extra_body:+ verbose_logger.debug(+ "fireworks_ai ignoring guided decoding params; explicit response_format takes precedence."+ )+ return ()if extra_body.get("guided_json") is not None:return (("response_format", _json_schema_response_format(extra_body["guided_json"])),)if extra_body.get("guided_grammar") is not None:@@ -429,13 +403,11 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):"grammar": extra_body["guided_grammar"],}return (("response_format", grammar_response_format),)- if extra_body.get("guided_choice") is not None:- choice_schema: Final = { # mutable-ok: JSON request body- "type": "string",- "enum": extra_body["guided_choice"],- }- return (("response_format", _json_schema_response_format(choice_schema)),)- return ()+ choice_schema: Final = { # mutable-ok: JSON request body+ "type": "string",+ "enum": extra_body["guided_choice"],+ }+ return (("response_format", _json_schema_response_format(choice_schema)),)def _transform_tools(self, tools: list[OpenAIChatCompletionToolParam]) -> list[OpenAIChatCompletionToolParam]:for tool in tools:diff --git a/tests/test_litellm/llms/fireworks_ai/chat/test_fireworks_ai_chat_transformation.py b/tests/test_litellm/llms/fireworks_ai/chat/test_fireworks_ai_chat_transformation.pyindex d25bbd69b91..48d868b5846 100644--- a/tests/test_litellm/llms/fireworks_ai/chat/test_fireworks_ai_chat_transformation.py+++ b/tests/test_litellm/llms/fireworks_ai/chat/test_fireworks_ai_chat_transformation.py@@ -1308,18 +1308,19 @@ def test_map_extra_body_params_translates_truncate_prompt_tokens():assert result == {"prompt_truncate_len": 4096}-def test_map_extra_body_params_truncate_prompt_tokens_conflicts_with_alias():+def test_map_extra_body_params_truncate_prompt_tokens_native_wins():config = FireworksAIConfig()- with pytest.raises(litellm.BadRequestError, match="aliases"):- config.map_extra_body_params(- {"prompt_truncate_len": 2048, "extra_body": {"truncate_prompt_tokens": 4096}},- _REASONING_MODEL,- )- with pytest.raises(litellm.BadRequestError, match="aliases"):- config.map_extra_body_params(- {"extra_body": {"truncate_prompt_tokens": 4096, "prompt_truncate_len": 2048}},- _REASONING_MODEL,- )+ top_level = config.map_extra_body_params(+ {"prompt_truncate_len": 2048, "extra_body": {"truncate_prompt_tokens": 4096}},+ _REASONING_MODEL,+ )+ assert top_level == {"prompt_truncate_len": 2048}++ nested = config.map_extra_body_params(+ {"extra_body": {"truncate_prompt_tokens": 4096, "prompt_truncate_len": 2048}},+ _REASONING_MODEL,+ )+ assert nested == {"extra_body": {"prompt_truncate_len": 2048}}def test_map_extra_body_params_chat_template_kwargs_enable_thinking():@@ -1337,28 +1338,29 @@ def test_map_extra_body_params_chat_template_kwargs_enable_thinking():assert enabled == {}-def test_map_extra_body_params_chat_template_kwargs_conflicts_with_reasoning_effort():+def test_map_extra_body_params_chat_template_kwargs_native_reasoning_effort_wins():config = FireworksAIConfig()- with pytest.raises(litellm.BadRequestError, match="enable_thinking"):- config.map_extra_body_params(- {- "reasoning_effort": "high",- "extra_body": {"chat_template_kwargs": {"enable_thinking": False}},- },- _REASONING_MODEL,- )+ result = config.map_extra_body_params(+ {+ "reasoning_effort": "high",+ "extra_body": {"chat_template_kwargs": {"enable_thinking": False}},+ },+ _REASONING_MODEL,+ )+ assert result == {"reasoning_effort": "high"}-def test_map_extra_body_params_chat_template_kwargs_conflicts_with_thinking():+def test_map_extra_body_params_chat_template_kwargs_native_thinking_wins():config = FireworksAIConfig()- with pytest.raises(litellm.BadRequestError, match="enable_thinking"):- config.map_extra_body_params(- {- "thinking": {"type": "enabled", "budget_tokens": 4096},- "extra_body": {"chat_template_kwargs": {"enable_thinking": True}},- },- _REASONING_MODEL,- )+ thinking = {"type": "enabled", "budget_tokens": 4096}+ result = config.map_extra_body_params(+ {+ "thinking": thinking,+ "extra_body": {"chat_template_kwargs": {"enable_thinking": True}},+ },+ _REASONING_MODEL,+ )+ assert result == {"thinking": thinking}def test_map_extra_body_params_chat_template_kwargs_dropped_for_non_reasoning_model():@@ -1370,6 +1372,15 @@ def test_map_extra_body_params_chat_template_kwargs_dropped_for_non_reasoning_moassert result == {}+def test_map_extra_body_params_non_dict_chat_template_kwargs_dropped():+ config = FireworksAIConfig()+ result = config.map_extra_body_params(+ {"extra_body": {"chat_template_kwargs": "enable_thinking"}},+ _REASONING_MODEL,+ )+ assert result == {}++def test_map_extra_body_params_guided_json():config = FireworksAIConfig()schema = {"type": "object", "properties": {"x": {"type": "string"}}}@@ -1401,25 +1412,37 @@ def test_map_extra_body_params_guided_grammar_and_choice():}-def test_map_extra_body_params_guided_conflicts_with_response_format():+def test_map_extra_body_params_guided_native_response_format_wins():config = FireworksAIConfig()- with pytest.raises(litellm.BadRequestError, match="response_format"):- config.map_extra_body_params(- {- "response_format": {"type": "json_object"},- "extra_body": {"guided_json": {"type": "object"}},- },- _REASONING_MODEL,- )+ top_level = config.map_extra_body_params(+ {+ "response_format": {"type": "json_object"},+ "extra_body": {"guided_json": {"type": "object"}},+ },+ _REASONING_MODEL,+ )+ assert top_level == {"response_format": {"type": "json_object"}}++ nested_format = {"type": "json_object"}+ nested = config.map_extra_body_params(+ {"extra_body": {"guided_json": {"type": "object"}, "response_format": nested_format}},+ _REASONING_MODEL,+ )+ assert nested == {"extra_body": {"response_format": nested_format}}-def test_map_extra_body_params_multiple_guided_params_rejected():+def test_map_extra_body_params_multiple_guided_params_priority_order():config = FireworksAIConfig()- with pytest.raises(litellm.BadRequestError, match="multiple guided decoding params"):- config.map_extra_body_params(- {"extra_body": {"guided_json": {"type": "object"}, "guided_grammar": "root ::= 'x'"}},- _REASONING_MODEL,- )+ result = config.map_extra_body_params(+ {"extra_body": {"guided_grammar": "root ::= 'x'", "guided_json": {"type": "object"}}},+ _REASONING_MODEL,+ )+ assert result == {+ "response_format": {+ "type": "json_schema",+ "json_schema": {"schema": {"type": "object"}},+ }+ }@pytest.mark.parametrize(