litellm/tests/unit/proxy/utils/proxy_logging
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
..
__init__.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00
_harness_smoke_test.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00
conftest.py test(proxy): delete the legacy proxy test tree and shard tests/unit/proxy by glob (#44018) 2026-10-01 11:40:45 -07:00
test_alerting.py feat(proxy): add team budget percentage email alerts (#52859) 2026-11-19 16:21:25 -08:00
test_callback_capabilities_class.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00
test_callback_capabilities_dataclass.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00
test_during_call_hook.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00
test_guardrail_pipeline.py fix(guardrails): check a model-level post_call response once across the proxy and deployment layers (#50300) 2026-11-07 07:34:06 +09:00
test_internal_usage_cache.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00
test_lifecycle.py fix(proxy): build configured logging callbacks at boot so a broken callback fails startup instead of the first request (#46378) 2026-10-14 16:51:55 +00:00
test_mcp_bridging.py feat(mcp): hand listed-tool description and input schema to pre-call hooks per caller (#41162) 2026-10-04 00:53:58 -07:00
test_module_helpers.py feat(auth): read virtual keys, jwt key mappings and model access groups through rust repositories (#52173) 2026-11-18 03:53:37 +00:00
test_post_call_failure_hook.py feat(guardrails): hold streamed tool calls for the verdict and log prompts blocked with a reply (#51872) 2026-11-20 16:58:15 -08:00
test_post_call_success_hook.py 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
test_pre_call_hook.py feat(callbacks-parallel-request-limiter): port parallel request limiter v3 to a rust callback (#51537) 2026-11-16 22:56:38 -08:00
test_streaming_hooks.py fix(logging): retain streaming success logging tasks until they finish (#52067) 2026-11-16 11:21:05 -08:00