Skip to content

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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into fireworks_nim_vllm_compat

mateo-berri9027ab94859a
diff --cc litellm/llms/fireworks_ai/chat/transformation.py
index 3965858d314,a796aa47b70..10aea51c833
--- a/litellm/llms/fireworks_ai/chat/transformation.py
+++ b/litellm/llms/fireworks_ai/chat/transformation.py
@@@ -61,61 -61,6 +61,61 @@@ def _extract_fireworks_hidden_params(pa
return {**top_level, **per_choice}
+def _json_schema_response_format(schema: object, name: str) -> Mapping[str, object]:
+ return {"type": "json_schema", "json_schema": {"name": name, "schema": schema}} # mutable-ok: JSON request body
+
+
- _EFFORT_KWARG_KEYS: Final = frozenset({"enable_thinking", "thinking", "reasoning_budget", "low_effort"})
++EFFORT_KWARG_KEYS: Final = frozenset({"enable_thinking", "thinking", "reasoning_budget", "low_effort"})
+
+
+def _bool_from_kwargs(kwargs: Mapping[str, object], keys: tuple[str, ...]) -> bool | None:
+ for key in keys:
+ value = kwargs.get(key)
+ if isinstance(value, bool):
+ return value
+ return None
+
+
- def _effort_from_chat_template_kwargs(kwargs: Mapping[str, object]) -> object:
++def effort_from_chat_template_kwargs(kwargs: Mapping[str, object]) -> object:
+ enable_thinking: Final = _bool_from_kwargs(kwargs, ("enable_thinking", "thinking"))
+ if enable_thinking is False:
+ return "none"
+ budget: Final = kwargs.get("reasoning_budget")
+ if isinstance(budget, (int, float)) and not isinstance(budget, bool) and budget > 0:
+ return int(budget)
+ low_effort: Final = _bool_from_kwargs(kwargs, ("low_effort",))
+ if low_effort is True:
+ return "low"
+ return None
+
+
- _NIM_VLLM_STRIP_PARAMS: Final = frozenset(
++NIM_VLLM_STRIP_PARAMS: Final = frozenset(
+ {
+ "stop_token_ids",
+ "include_stop_str_in_output",
+ "skip_special_tokens",
+ "spaces_between_special_tokens",
+ "best_of",
+ "use_beam_search",
+ "guided_decoding_backend",
+ "guided_regex",
+ "add_generation_prompt",
+ "continue_final_message",
+ "add_special_tokens",
+ "detokenize",
+ "allowed_token_ids",
+ "bad_words",
+ "include_reasoning",
+ "nvext",
+ }
+)
+
+_EXTRA_BODY_CONSUMED_PARAMS: Final = (
+ frozenset({"truncate_prompt_tokens", "chat_template_kwargs", "guided_json", "guided_grammar", "guided_choice"})
- | _NIM_VLLM_STRIP_PARAMS
++ | NIM_VLLM_STRIP_PARAMS
+)
+
+
class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):
"""
Reference: https://docs.fireworks.ai/api-reference/post-chatcompletions
@@@ -328,119 -273,6 +328,119 @@@
return optional_params
+ def map_extra_body_params(
+ self, optional_params: Mapping[str, object], model: str
+ ) -> dict: # mutable-ok: http handler pops extra_body off the returned dict
+ extra_body: Final = optional_params.get("extra_body")
+ if not isinstance(extra_body, dict):
+ return dict(optional_params) # mutable-ok: JSON request body
+
- stripped: Final = tuple(sorted(k for k in extra_body if k in _NIM_VLLM_STRIP_PARAMS))
++ stripped: Final = tuple(sorted(k for k in extra_body if k in NIM_VLLM_STRIP_PARAMS))
+ if stripped:
+ verbose_logger.debug(
+ "fireworks_ai does not support NIM/vLLM params %s for model=%s; dropping them from the request.",
+ stripped,
+ model,
+ )
+ promoted: Final = (
+ *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),
++ *self.translate_guided_params(extra_body, optional_params),
+ )
+ if "response_format" in extra_body and "response_format" in optional_params:
+ verbose_logger.debug(
+ "fireworks_ai dropping extra_body.response_format; the top-level response_format takes precedence."
+ )
+ remaining: Final = tuple(
+ (k, v)
+ for k, v in extra_body.items()
+ if k not in _EXTRA_BODY_CONSUMED_PARAMS
+ and (k != "response_format" or "response_format" not in optional_params)
+ )
+ base: Final = {k: v for k, v in optional_params.items() if k != "extra_body"} # mutable-ok: JSON request body
+ return { # mutable-ok: JSON request body
+ **base,
+ **dict(promoted), # mutable-ok: JSON request body
+ **({"extra_body": dict(remaining)} if remaining else {}), # mutable-ok: JSON request body
+ }
+
+ @staticmethod
+ 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], 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):
+ 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 not in _EFFORT_KWARG_KEYS))
++ other_keys: Final = tuple(sorted(k for k in chat_template_kwargs if k not in EFFORT_KWARG_KEYS))
+ if other_keys:
+ verbose_logger.debug(
+ "fireworks_ai does not support chat_template_kwargs keys %s for model=%s; dropping them.",
+ other_keys,
+ model,
+ )
+ if any(key in optional_params or key in extra_body for key in ("reasoning_effort", "thinking")):
+ verbose_logger.debug(
+ "fireworks_ai ignoring chat_template_kwargs; explicit reasoning_effort/thinking takes precedence."
+ )
+ return ()
- effort: Final = _effort_from_chat_template_kwargs(chat_template_kwargs)
++ effort: Final = effort_from_chat_template_kwargs(chat_template_kwargs)
+ if effort is None:
+ 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 effort keys.",
+ model,
+ )
+ return ()
+ return (("reasoning_effort", effort),)
+
+ @staticmethod
- def _translate_guided_params(
++ 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"], "response")),)
+ if extra_body.get("guided_grammar") is not None:
+ grammar_response_format: Final = { # mutable-ok: JSON request body
+ "type": "grammar",
+ "grammar": extra_body["guided_grammar"],
+ }
+ return (("response_format", grammar_response_format),)
+ choice_schema: Final = { # mutable-ok: JSON request body
+ "type": "string",
+ "enum": extra_body["guided_choice"],
+ }
+ return (("response_format", _json_schema_response_format(choice_schema, "choice")),)
+
def _transform_tools(self, tools: list[OpenAIChatCompletionToolParam]) -> list[OpenAIChatCompletionToolParam]:
for tool in tools:
if tool.get("type") != "function":
diff --cc litellm/llms/fireworks_ai/completion/transformation.py
index 7e72d1c3fa6,c141e097d3a..594080beaab
--- a/litellm/llms/fireworks_ai/completion/transformation.py
+++ b/litellm/llms/fireworks_ai/completion/transformation.py
@@@ -7,18 -4,8 +7,18 @@@ from litellm.utils import supports_reas
from ...base_llm.completion.transformation import BaseTextCompletionConfig
from ...openai.completion.utils import _transform_prompt
+from ..chat.transformation import (
- _EFFORT_KWARG_KEYS,
- _NIM_VLLM_STRIP_PARAMS,
++ EFFORT_KWARG_KEYS,
++ NIM_VLLM_STRIP_PARAMS,
+ FireworksAIConfig,
- _effort_from_chat_template_kwargs,
++ effort_from_chat_template_kwargs,
+)
from ..common_utils import FireworksAIMixin
+_TEXT_COMPLETION_STRIP_PARAMS: Final = (
- frozenset({"truncate_prompt_tokens", "prompt_truncate_len"}) | _NIM_VLLM_STRIP_PARAMS
++ frozenset({"truncate_prompt_tokens", "prompt_truncate_len"}) | NIM_VLLM_STRIP_PARAMS
+)
+
class FireworksAITextCompletionConfig(FireworksAIMixin, BaseTextCompletionConfig):
def get_supported_openai_params(self, model: str) -> list:
@@@ -54,107 -41,6 +54,107 @@@
optional_params[k] = v
return optional_params
+ def map_extra_body_params(
+ self, optional_params: Mapping[str, object], model: str
+ ) -> dict: # mutable-ok: returned dict is spread into the OpenAI SDK call as kwargs
+ raw_extra_body: Final = optional_params.get("extra_body")
+ initial_body: Final = (
+ dict(raw_extra_body) if isinstance(raw_extra_body, dict) else {} # mutable-ok: JSON request body
+ )
+ stripped_body: Final = self._strip_unsupported_params(initial_body, model)
+ moved_body: Final = self._move_native_params_into_extra_body(stripped_body, optional_params)
+ effort_body: Final = self._translate_chat_template_kwargs(moved_body, optional_params, model)
+ final_body: Final = self._translate_guided_into_extra_body(effort_body, optional_params)
+ base: Final = { # mutable-ok: JSON request body
+ k: v for k, v in optional_params.items() if k not in ("extra_body", "response_format", "reasoning_effort")
+ }
+ if final_body:
+ base["extra_body"] = final_body
+ return base
+
+ @staticmethod
+ def _strip_unsupported_params(
+ extra_body: Mapping[str, object], model: str
+ ) -> dict: # mutable-ok: JSON request body
+ stripped: Final = tuple(sorted(k for k in extra_body if k in _TEXT_COMPLETION_STRIP_PARAMS))
+ if stripped:
+ verbose_logger.debug(
+ "fireworks_ai does not support NIM/vLLM params %s for model=%s; dropping them from the request.",
+ stripped,
+ model,
+ )
+ return { # mutable-ok: JSON request body
+ k: v for k, v in extra_body.items() if k not in _TEXT_COMPLETION_STRIP_PARAMS
+ }
+
+ @staticmethod
+ def _move_native_params_into_extra_body(
+ extra_body: Mapping[str, object], optional_params: Mapping[str, object]
+ ) -> dict: # mutable-ok: JSON request body
+ moved: Final = dict(extra_body) # mutable-ok: JSON request body
+ for key in ("response_format", "reasoning_effort"):
+ value = optional_params.get(key)
+ if value is None:
+ continue
+ if key in moved:
+ verbose_logger.debug("fireworks_ai overriding extra_body.%s with the top-level %s.", key, key)
+ moved[key] = value
+ return moved
+
+ def _translate_chat_template_kwargs(
+ self, extra_body: Mapping[str, object], optional_params: Mapping[str, object], model: str
+ ) -> dict: # mutable-ok: JSON request body
+ chat_template_kwargs: Final = extra_body.get("chat_template_kwargs")
+ if chat_template_kwargs is None:
+ return dict(extra_body) # mutable-ok: JSON request body
+ result: Final = { # mutable-ok: JSON request body
+ k: v for k, v in extra_body.items() if k != "chat_template_kwargs"
+ }
+ if not isinstance(chat_template_kwargs, dict):
+ verbose_logger.debug(
+ "fireworks_ai dropping chat_template_kwargs for model=%s; expected an object, got %s.",
+ model,
+ type(chat_template_kwargs).__name__,
+ )
+ return result
- other_keys: Final = tuple(sorted(k for k in chat_template_kwargs if k not in _EFFORT_KWARG_KEYS))
++ other_keys: Final = tuple(sorted(k for k in chat_template_kwargs if k not in EFFORT_KWARG_KEYS))
+ if other_keys:
+ verbose_logger.debug(
+ "fireworks_ai does not support chat_template_kwargs keys %s for model=%s; dropping them.",
+ other_keys,
+ model,
+ )
- effort: Final = _effort_from_chat_template_kwargs(chat_template_kwargs)
++ effort: Final = effort_from_chat_template_kwargs(chat_template_kwargs)
+ if effort is None:
+ return result
+ if any(key in result or key in optional_params for key in ("reasoning_effort", "thinking")):
+ verbose_logger.debug(
+ "fireworks_ai ignoring chat_template_kwargs; explicit reasoning_effort/thinking takes precedence."
+ )
+ return result
+ 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 effort keys.",
+ model,
+ )
+ return result
+ return {**result, "reasoning_effort": effort} # mutable-ok: JSON request body
+
+ @staticmethod
+ def _translate_guided_into_extra_body(
+ extra_body: Mapping[str, object], optional_params: Mapping[str, object]
+ ) -> dict: # mutable-ok: JSON request body
- guided_response_format: Final = FireworksAIConfig._translate_guided_params(extra_body, optional_params)
++ guided_response_format: Final = FireworksAIConfig.translate_guided_params(extra_body, optional_params)
+ remaining: Final = { # mutable-ok: JSON request body
+ k: v for k, v in extra_body.items() if k not in ("guided_json", "guided_grammar", "guided_choice")
+ }
+ if guided_response_format:
+ return { # mutable-ok: JSON request body
+ **remaining,
+ guided_response_format[0][0]: guided_response_format[0][1],
+ }
+ return remaining
+
def transform_text_completion_request(
self,
model: str,