refactor(guardrails): narrow the SSE error-frame helper to the input it actually takes

Both callers pass a string, so the Mapping overload and its json.dumps branch
were unreachable. Folds the block branch's narrative comment into the rebind
suppressions that already carry a reason.
This commit is contained in:
Yucheng Zhu 2026-08-12 20:31:19 -07:00
parent f0da259f66
commit 6764678925
2 changed files with 5 additions and 11 deletions

View file

@ -98,15 +98,13 @@ def model_response_text(response: ModelResponse) -> str:
)
def anthropic_sse_error_frames(message: str | Mapping[str, object]) -> tuple[bytes, ...]:
def anthropic_sse_error_frames(message: str) -> tuple[bytes, ...]:
"""Anthropic error event, for a failure discovered after the response headers were flushed.
Once a keepalive ping has been sent a raise cannot reach the client, so the failure has to
travel as a frame. A structured detail is JSON-serialized into the string so it stays machine
readable without violating the string-typed contract.
travel as a frame.
"""
text: Final = message if isinstance(message, str) else json.dumps(message, default=str)
body: Final = json.dumps(text)
body: Final = json.dumps(message)
return (
f'event: error\ndata: {{"type": "error", "error": {{"type": "guardrail_error", '
f'"message": {body}}}}}\n\n'.encode(),

View file

@ -2639,13 +2639,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
return
except ModifyResponseException as e:
if raw_sse:
# the shared per-endpoint block builder the unified guardrail path uses; it
# mints a fresh message id because the block is not the upstream message, but
# it reports exc.model, which defaults to the guardrail name rather than the model
e.model = _pre_block_response.model or e.model # rebind-ok: local synthetic block payload
e.model = _pre_block_response.model or e.model # rebind-ok: exc.model defaults to the guardrail
if e.original_response is None:
# the builder reads usage off this; the upstream call was already paid for
e.original_response = _pre_block_response # rebind-ok: local synthetic block payload
e.original_response = _pre_block_response # rebind-ok: the block builder reads usage off this
for block_chunk in AnthropicMessagesHandler().build_block_sse_chunks(e, stream_started=False):
yield block_chunk
return