Repository navigation
feat: support per-request enable_json_schema_validation for thread safety - #21233
Conversation
…fety Allow passing enable_json_schema_validation as a parameter to completion() and acompletion() instead of only relying on the global litellm.enable_json_schema_validation flag. The per-request value takes priority when provided; otherwise falls back to the global (backward compatible). This makes JSON schema validation safe for concurrent usage in FastAPI and other multi-threaded environments.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR adds per-request
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/main.py | Adds enable_json_schema_validation as an optional parameter to both completion() and acompletion(), and forwards it through completion_kwargs and the MCP handler call. Clean, minimal changes. |
| litellm/types/utils.py | Registers enable_json_schema_validation in all_litellm_params to prevent the parameter from leaking to provider APIs. Single-line addition. |
| litellm/utils.py | Updates post_call_processing to check per-request enable_json_schema_validation from optional_params before falling back to the global flag. Logic is correct and backward-compatible. |
| tests/litellm/litellm_core_utils/test_json_schema_validation.py | Comprehensive test suite covering all 4 combinations of global/per-request flags, valid response pass-through, and param registration. No real network calls—uses mock data directly. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["completion() / acompletion()"] -->|"enable_json_schema_validation param"| B["@client decorator wrapper"]
B --> C["post_call_processing()"]
C --> D{"Per-request flag\nin optional_params?"}
D -->|Yes| E["Use per-request value"]
D -->|No / None| F["Use global\nlitellm.enable_json_schema_validation"]
E --> G{"Flag is True?"}
F --> G
G -->|Yes| H["Validate response\nagainst JSON schema"]
G -->|No| I["Skip validation"]
H -->|Invalid| J["Raise JSONSchemaValidationError"]
H -->|Valid| K["Continue normally"]
Last reviewed commit: 909e3ce
Additional Comments (1)
When This means per-request JSON schema validation will not work when MCP tools are present. Add |
The parameter was declared in completion() signature but not passed to acompletion_with_mcp, causing per-request JSON schema validation to silently fall back to the global default when MCP tools are present.
d384f7c
into
BerriAI:litellm_oss_staging_03_03_2026
…chema-validation feat: support per-request enable_json_schema_validation for thread safety
Relevant issues
Fixes thread-safety issue with
litellm.enable_json_schema_validationglobal flag in concurrent environments (FastAPI, multi-threaded services).Pre-Submission checklist
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🆕 New Feature
Changes
Allow passing
enable_json_schema_validationas a per-request parameter tocompletion()andacompletion(), instead of only relying on the globallitellm.enable_json_schema_validationflag.The global flag isn't thread-safe. In a FastAPI service with concurrent users, one request can change the flag while another is mid-execution:
Solution: Pass the flag dynamically per-request:
Priority: per-request value > global value. If not passed, falls back to the global (fully backward compatible).
Files changed:
litellm/main.py— addedenable_json_schema_validationparam tocompletion()andacompletion()litellm/types/utils.py— registered inall_litellm_paramsso it doesn't leak to provider APIslitellm/utils.py—post_call_processing()checks per-request value first, falls back to globaltests/litellm/litellm_core_utils/test_json_schema_validation.py— 6 tests covering the full matrix