litellm/tests/unit/proxy/utils
drewv19 1e0b60f4ca
fix(guardrails): clear token ids and logprobs when a post-call hook rewrites a non-streamed answer (#53758)
* fix(guardrails): drop vLLM token ids from non-streamed choices a post-call hook rewrote

A masking post_call guardrail rewrote the answer text but left the hosted_vllm
token_ids that decode back to the unmasked words, in the response and in the
logged payload. Compare each choice that carries token ids before and after the
post-call hooks, and set its token_ids to null when its output changed. The
logging overlay that swaps in the served text drops the logged ids the same way.

* fix(guardrails): also drop logprobs, cover model-level guardrails and dict callback responses

A deployment-level guardrail masks the answer inside the SDK's deployment hook, before the proxy takes its snapshot, so the proxy saw no change and kept the token ids. The deployment hook now takes the same snapshot and drops token ids and logprobs from every choice it rewrote.

logprobs carry the original tokens as plain text, so a rewritten non-streamed choice now drops them too, in the response and in the logged copy.

A callback can return a plain dict, or edit a passthrough dict in place. The snapshot and the drop now read and write dict choices, and the snapshot deep-copies a dict message so an in-place edit still counts as a rewrite.

* fix(guardrails): keep token data on attach-only hooks, skip it in the guardrail fingerprint, and clear Responses API logprobs

* perf(guardrails): skip token data before hashing the guardrail fingerprint, and test every answer field

* fix(guardrails): keep token data when a post-call hook only appends text, and hash only choices and output in the fingerprint

* test(guardrails): type the post-call test callbacks' hook signatures

* fix(guardrails): pair each served answer with its original by token data, not list position

* fix(guardrails): pair answers by token ids before logprobs and check token data after every post-call hook

* fix(guardrails): check token data after each V2 callback patch and clear ambiguous logprob matches

A CustomLoggerV2 callback that masks an answer an earlier V2 callback patched in
left the new answer's token ids readable, because the check only compared the
response before and after the whole callback chain. Each patch is now checked on
its own on both the proxy and the deployment path.

A served choice whose logprob values match more than one original now keeps its
token data only when it kept every one of those answers.

* fix(guardrails): pair token ids found in any token id field of the original answer

A post-call hook that masked an answer and moved its token ids to another
field (choice, choice provider fields, or message provider fields) left
ids that matched no original, so they were kept and still decoded the
hidden text. Ids now pair when any id list on the served choice matches
any id list on the original.

* fix(guardrails): pair logprob-only answers by token bytes and text offsets too

A callback that asks the model again could get a fresh answer whose rounded
logprob values equal the original's (all 0.0 for a short, confident answer).
Pairing on values alone matched it to the original, saw different text and
cleared the fresh answer's logprobs. The logprob key now also holds each
token's byte list (chat and Responses) and the text offsets (/v1/completions),
which a text mask leaves unchanged but a different answer does not share.

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-24 06:16:09 +00:00
..
helpers feat(proxy)!: make Scalar the default API reference and remove ReDoc (#53063) 2026-11-23 10:49:54 -08:00
prisma_and_spend perf(proxy): coalesce empty auto-router baseline recovery polls (#52523) 2026-11-19 00:20:59 +00:00
proxy_logging fix(guardrails): clear token ids and logprobs when a post-call hook rewrites a non-streamed answer (#53758) 2026-11-24 06:16:09 +00:00
__init__.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00