litellm/tests/unit/responses/mcp
edvinsq 3a7ccf9a51
fix(logging): keep tracebacks in one JSON record and log the token count skip at debug (#54603)
* fix(logging): keep each proxy traceback in one JSON record and log token-count skips at debug

With JSON logs on, exceptions raised in a thread or a destructor, and the
aiohttp unclosed-session warning, printed as plain multi-line text, so a log
collector split each traceback into one error entry per line. The thread and
unraisable hooks now write one JSON record each, env-only JSON_LOGS sets up
JSON logging the way the json_logs config key does, and the pre-call token
count skip logs at DEBUG like the other skips in the same check.

* fix(logging): leave operator root handlers alone when JSON logs come from the env

Running the full JSON setup when proxy_server imports replaced the root handlers that an operator's --log_config or OpenTelemetry had installed. JSON mode now swaps Python's logging.lastResort for the JSON handler at WARNING instead. Python only uses it when no handler takes a record, so asyncio and aiohttp warnings still come out as one JSON record and configured handlers keep getting theirs

* fix(logging): write JSON hook records to the current stderr

The JSON handler behind the exception hooks and logging.lastResort kept the
stderr that existed when JSON logs turned on, so a caller that redirected
sys.stderr later lost thread tracebacks, destructor errors and unhandled
library warnings. It now resolves stderr per record, like Python's defaults
and litellm's own log handler

* fix(logging): keep host hooks, exit-time destructor errors and whole hook tracebacks

Install the JSON hooks only where Python's defaults are still in place, fall back to Python's own
unraisable hook while the interpreter shuts down, keep hook tracebacks untruncated, drop the active
request's trace id from destructor records, name the raising file and line in the logger field, and
log the Responses MCP stream errors through the logger instead of traceback.print_exc()

* fix(logging): keep JSON crash output under ddtrace-run, cap hook records and drop trace ids from GC records

JSON logs take over sys.excepthook as before and still run a hook another tool set first, such as
ddtrace-run's, with the plain traceback it hands to Python dropped. Hook records follow
MAX_STRING_LENGTH_STDOUT_LOG again, and records no handler takes carry no trace id. The MCP stream
test now fails the follow-up call against a closed local port instead of replacing aresponses.

* fix(logging): replace sys.excepthook as before instead of muting stderr for the replaced hook

Running the replaced hook with process-wide stderr redirected also dropped log lines other threads
wrote meanwhile, so JSON logs go back to replacing sys.excepthook outright, as the merge base does.

* test(mcp): check every MCP stream failure site logs one JSON record

* test(mcp): stub the failing model call instead of opening a loopback socket

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-30 18:19:05 +00:00
..
__init__.py ci: move provider-independent MCP tests into tests/unit and run mcp-integration from litellm-tests (#42904) 2026-09-24 23:07:48 +00:00
conftest.py refactor(mcp): drop the experimental label from the MCP server and client paths (#48513) 2026-10-27 17:47:50 +00:00
test_aresponses_api_with_mcp.py feat(responses): store each bridged turn's path to the root so a fork replays only its ancestors (#49519) 2026-11-23 12:43:11 -08:00
test_chat_completions_handler.py fix(mcp): drop hosted MCP tool_choice under drop_params and honor drop_params in Bedrock batch rows (#53697) 2026-11-23 08:51:59 -08:00
test_litellm_proxy_mcp_handler.py fix(proxy): keep a request's slot, TPM and budget holds while it runs MCP tools (#54469) 2026-11-26 08:11:44 +00:00
test_mcp_streaming_iterator.py fix(logging): keep tracebacks in one JSON record and log the token count skip at debug (#54603) 2026-11-30 18:19:05 +00:00
test_parked_stream_dispatch.py fix(mcp): bill both calls of a streamed chat completions MCP auto-execute (#48899) 2026-10-30 07:34:10 -07:00
test_round_billing.py fix(responses): bill the provider-reported input on a cut bridged stream (#53394) 2026-11-23 06:20:25 +00:00
test_tool_precheck.py feat(messages): serve anthropic messages on rust only (#51119) 2026-11-12 00:07:20 +00:00