forked from BerriAI/litellm
-
Notifications
You must be signed in to change notification settings - Fork 9
Fix JSON logging for asyncio exceptions #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Feature] Allow Error code filtering on Spend Logs
…ner-integration feat(databricks): Add enhanced authentication, security features, and custom user-agent support
…rror_code [Feature] UI - Add Error Code Filtering on UI
[Fix] UI - Key Creation MCP Settings Submit Form Unintentionally
…-window-error fix(gemini): properly catch context window exceeded errors
…issue fix: lost tool_calls when streaming has both text and tool_calls
fix: remove deprecated Groq models and update model registry
* Add 5 AI providers using `openai_like`: * Synthetic.new * Apertis / Stima.tech * NanoGPT * Poe * Chutes.ai * Update additional missing locations
…8347) Add missing pricing entries for azure/gpt-image-1.5 and azure/gpt-image-1.5-2025-12-16 to model_prices_and_context_window.json. These models use token-based pricing (same as OpenAI): - Text input: $5.00/1M tokens - Image input: $8.00/1M tokens - Image output: $32.00/1M tokens - Cached text: $1.25/1M tokens - Cached image: $2.00/1M tokens
…AI#18346) Remove 'none' from gpt-5-mini's supported reasoning_effort values in the documentation table. gpt-5-mini does not support reasoning_effort="none", only minimal, low, medium, and high.
Added pricing and configuration details for the azure_ai/gpt-oss-120b model, including costs and capabilities.
… sensitive information Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
* Allow get_nested_value dot notation to support escaping for Kubernetes JWT Support * Add support for team and org alias fields, add docs, tests * Fix lint issue with max statements in handle jwt logic
…odel-name fix: unify model names to provider-defined names
…_server_to_playground add selectable mcp servers to the playground
…taurl_on_ui feat: add UI support for configuring meta URLs
…ic-image-urls fix(vertex_ai): convert image URLs to base64 for Vertex AI Anthropic
…e-type-per-object fix(vertex_ai): separate Tool objects for each tool type per API spec
…er-ui feat: Add MiniMax provider support to UI dashboard
…ool-result-support Fix Gemini 3 imgs in tool response
fix: correct deepseek-v3p2 pricing for Fireworks AI
…track Add all resolution for gpt-image-1.5
…nstructions Preserve system instructions for gemini
…_call_thought_sign Add thought signature for non tool call requests
…haching_header Remove prompt caching headers as the support has been removed
…budget Add validation for negative budget
fix background cost tracking tests
…n-based pricing (BerriAI#17906) * fix(cost_calculator): correct gpt-image-1 cost calculation using token-based pricing (BerriAI#13847) gpt-image-1 uses token-based pricing (like chat models), not pixel-based pricing like DALL-E. The old code was calculating incorrect costs by treating it as DALL-E. Changes: - Update model pricing JSON with correct token-based costs for gpt-image-1 - Add dedicated cost calculator for OpenAI gpt-image models - Route gpt-image-1 to token-based calculator in cost router - Add comprehensive tests for the new calculator * refactor: simplify gpt-image-1 cost calculator using responses API helper Reuse _transform_response_api_usage_to_chat_usage and generic_cost_per_token for gpt-image-1 cost calculation since ImageUsage has the same spec as ResponseAPIUsage.
The asyncio exception handler was being set on the wrong event loop at module import time. When uvicorn creates a new event loop, the handler was not set on it, causing asyncio exceptions (like 'Task exception was never retrieved') to be logged as plain text with each line as a separate log entry. Changes: - Add _setup_asyncio_json_exception_handler() function that sets the exception handler on the running event loop - Call this function in proxy_startup_event after uvicorn creates the event loop - Include full traceback in exc_info by extracting it from the exception object - Handle non-exception errors by logging them as JSON too This ensures that asyncio exceptions are logged as single JSON objects with the stacktrace field, making them easier to parse in Datadog. Co-authored-by: openhands <openhands@all-hands.dev>
Adds log_format parameter supporting json_array (default), ndjson, and single formats. NDJSON format enables webhook integrations like Sumo Logic to parse individual log records at ingest time. Defaults to json_array for backward compatibility.
|
Looks like there are a few issues preventing this PR from being merged!
If you'd like me to help, just leave a comment, like Feel free to include any additional details that might help me get this PR into a better state. You can manage your notification settings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When
JSON_LOGS=trueis set, asyncio exceptions (like "Task exception was never retrieved") were being logged as plain text with each line of the traceback as a separate log entry. This made it difficult to parse and analyze errors in Datadog.Example of the problem in Datadog:
Notice that:
levelortimestampattributes (indicating JSON formatter is not being used)stacktracefieldRoot Cause
The asyncio exception handler was being set on the wrong event loop at module import time. When
litellm/_logging.pyis imported, it callsasyncio.get_event_loop().set_exception_handler(). However, at import time, the event loop may not be the one that uvicorn will use. When uvicorn creates a new event loop, the exception handler is not set on it.Additionally, the original
async_json_exception_handlerwas settingexc_info=None, which meant the traceback was not being included in the JSON output.Solution
Add
_setup_asyncio_json_exception_handler()function - This function sets the asyncio exception handler on the running event loop. It should be called AFTER the event loop is created by uvicorn.Call the function in
proxy_startup_event- This ensures the handler is set on the correct event loop that uvicorn uses.Include full traceback in
exc_info- Extract the traceback from the exception object usingexception.__traceback__and pass it to the LogRecord.Handle non-exception errors - Log them as JSON too instead of falling back to the default handler.
Expected Result
After this fix, asyncio exceptions will be logged as single JSON objects with the stacktrace field:
{ "message": "Task exception was never retrieved", "level": "ERROR", "timestamp": "2026-01-02T17:57:31.144636", "stacktrace": "Traceback (most recent call last):\n File \"/tmp/test.py\", line 68, in failing_task\n raise RuntimeError(\"Async task failed!\")\nRuntimeError: Async task failed!" }Testing
Verified with a standalone test that:
@neubig can click here to continue refining the PR