fix(headroom): drop stream options after CCR conversion

This commit is contained in:
IvanShang 2026-09-07 09:21:54 +08:00
parent 168a0055a2
commit 6584fd75c4
2 changed files with 7 additions and 2 deletions

View file

@ -890,7 +890,7 @@ class HeadroomGuardrail(CustomGuardrail):
if not has_headroom_retrieve_tool(effective.get("tools")):
return base_result
return { # mutable-ok: the hook contract is a plain dict the router merges into the request kwargs
**effective,
**{key: value for key, value in effective.items() if key != "stream_options"}, # mutable-ok: removes stream-only options
"stream": False,
HEADROOM_CONVERTED_STREAM_KEY: True,
}

View file

@ -2165,7 +2165,7 @@ async def test_pre_call_deployment_hook_converts_stream_only_for_ccr_chat_comple
tools: Optional[list],
expect_conversion: bool,
):
kwargs = {"model": "gpt-4o", "stream": stream, "tools": tools}
kwargs = {"model": "gpt-4o", "stream": stream, "stream_options": {"include_usage": True}, "tools": tools}
result = await guardrail.async_pre_call_deployment_hook(kwargs=kwargs, call_type=call_type)
@ -2173,12 +2173,15 @@ async def test_pre_call_deployment_hook_converts_stream_only_for_ccr_chat_comple
assert result is kwargs
assert HEADROOM_CONVERTED_STREAM_KEY not in kwargs
assert kwargs["stream"] is stream
assert kwargs["stream_options"] == {"include_usage": True}
return
assert result is not None
assert result["stream"] is False
assert "stream_options" not in result
assert result[HEADROOM_CONVERTED_STREAM_KEY] is True
assert kwargs["stream"] is True
assert kwargs["stream_options"] == {"include_usage": True}
@pytest.mark.asyncio
@ -2233,6 +2236,7 @@ async def test_pre_call_deployment_hook_converts_stream_after_deployment_level_c
"model": "gpt-4o",
"messages": [dict(m) for m in ORIGINAL_MESSAGES],
"stream": True,
"stream_options": {"include_usage": True},
"guardrails": ["headroom"],
"metadata": {},
}
@ -2248,6 +2252,7 @@ async def test_pre_call_deployment_hook_converts_stream_after_deployment_level_c
assert result is not None
assert has_headroom_retrieve_tool(result["tools"])
assert result["stream"] is False
assert "stream_options" not in result
assert result[HEADROOM_CONVERTED_STREAM_KEY] is True