fix(fireworks_ai): strip cache_control on a copy so a fallback keeps the caller's hints

This commit is contained in:
mateo-berri 2026-10-29 10:23:15 -07:00
parent 4a933bf1d5
commit ddedcfe58a
3 changed files with 25 additions and 8 deletions

View file

@ -44,6 +44,7 @@ from ...openai.chat.gpt_transformation import (
OpenAIChatCompletionStreamingHandler,
OpenAIGPTConfig,
)
from ...openai.common_utils import without_cache_control
from ..common_utils import (
FIREROUTER,
FireworksAIException,
@ -490,10 +491,6 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):
"""
Strip fields not permitted by FireworksAI from messages.
"""
from litellm.litellm_core_utils.prompt_templates.common_utils import (
filter_value_from_dict,
)
supports_vision_value: Final = self._get_model_cost_capability_exact(model=model, capability="supports_vision")
for message in messages:
if message["role"] == "user":
@ -524,9 +521,11 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):
model=model,
llm_provider="fireworks_ai",
)
filter_value_from_dict(cast(dict, message), "cache_control")
return [strip_litellm_response_only_fields(message) for message in messages]
return [
strip_litellm_response_only_fields(cast(AllMessageValues, without_cache_control(message)))
for message in messages
]
# Cached index of fireworks_ai/* entries from litellm.model_cost. Building
# this index requires a full scan of model_cost (tens of thousands of

View file

@ -51,8 +51,6 @@ def _is_list(value: object) -> bool:
def _contains_cache_control(value: object) -> bool:
# Iterative rather than recursive: the code-quality gate bans unignored recursion here,
# and request payloads are attacker-shaped, so an explicit worklist has no stack depth to blow.
pending: Final[list[object]] = [value] # mutable-ok: a local worklist, never returned or stored
while pending:
node = pending.pop()

View file

@ -535,6 +535,26 @@ def test_tool_search_reply_sent_to_fireworks_drops_litellm_response_fields():
}
def test_cache_control_is_stripped_from_the_request_but_kept_on_the_callers_messages():
hint: Final = {"type": "ephemeral"}
messages: Final = [
{"role": "system", "content": [{"type": "text", "text": "a long cached prompt", "cache_control": hint}]},
{"role": "user", "content": "hi", "cache_control": hint},
]
caller_messages_before: Final = json.dumps(messages)
request: Final = FireworksAIConfig().transform_request(
model="accounts/fireworks/models/gpt-oss-120b",
messages=messages,
optional_params={},
litellm_params={},
headers={},
)
assert "cache_control" not in json.dumps(request["messages"])
assert json.dumps(messages) == caller_messages_before
# -----------------------------------------------------------------------------
# Regression tests for legacy / OpenAPI $ref defs in tool parameters.
#