Skip to content

Commit 5120a83

Browse files
author
Adrian Lyjak
committed
fix #9692. Return a copy from strict key removal to not break cache keys
1 parent 655ce2e commit 5120a83

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

litellm/utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,23 +2770,33 @@ def _remove_additional_properties(schema):
27702770

27712771
def _remove_strict_from_schema(schema):
27722772
"""
2773-
Relevant Issues: https://github.com/BerriAI/litellm/issues/6136, https://github.com/BerriAI/litellm/issues/6088
2773+
Recursively removes 'strict' from schema. Returns a copy, in order to not break cache keys, (so you should update your reference)
2774+
2775+
Relevant Issues: https://github.com/BerriAI/litellm/issues/6136, https://github.com/BerriAI/litellm/issues/6088,
27742776
"""
2777+
maybe_copy = None # make a copy to not break cache keys https://github.com/BerriAI/litellm/issues/9692
27752778
if isinstance(schema, dict):
27762779
# Remove the 'additionalProperties' key if it exists and is set to False
27772780
if "strict" in schema:
2778-
del schema["strict"]
2781+
maybe_copy = schema.copy()
2782+
del maybe_copy["strict"]
27792783

27802784
# Recursively process all dictionary values
27812785
for key, value in schema.items():
2782-
_remove_strict_from_schema(value)
2786+
result = _remove_strict_from_schema(value)
2787+
if result is not value:
2788+
maybe_copy = maybe_copy or schema.copy()
2789+
maybe_copy[key] = result
27832790

27842791
elif isinstance(schema, list):
27852792
# Recursively process all items in the list
2786-
for item in schema:
2787-
_remove_strict_from_schema(item)
2793+
for i, item in enumerate(schema):
2794+
result = _remove_strict_from_schema(item)
2795+
if result is not item:
2796+
maybe_copy = maybe_copy or list(schema)
2797+
maybe_copy[i] = result
27882798

2789-
return schema
2799+
return maybe_copy or schema
27902800

27912801

27922802
def _remove_unsupported_params(

tests/litellm_utils_tests/test_utils.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import json
23
import sys
34
import time
45
from datetime import datetime
@@ -2102,3 +2103,70 @@ def test_get_provider_audio_transcription_config():
21022103
config = ProviderConfigManager.get_provider_audio_transcription_config(
21032104
model="whisper-1", provider=provider
21042105
)
2106+
2107+
def test_remove_strict_from_schema():
2108+
from litellm.utils import _remove_strict_from_schema
2109+
2110+
schema = { # This isn't maybe actually very realistic json schema, just slop full of stricts
2111+
"$schema": "http://json-schema.org/draft-07/schema#",
2112+
"type": "object",
2113+
"strict": True,
2114+
"definitions": {
2115+
"address": {
2116+
"type": "object",
2117+
"properties": {
2118+
"street": {"type": "string"},
2119+
"city": {"type": "string"}
2120+
},
2121+
"required": ["street", "city"],
2122+
"strict": True
2123+
}
2124+
},
2125+
"properties": {
2126+
"name": {
2127+
"type": "string",
2128+
"strict": True
2129+
},
2130+
"age": {
2131+
"type": "integer"
2132+
},
2133+
"address": {
2134+
"$ref": "#/definitions/address"
2135+
},
2136+
"tags": {
2137+
"type": "array",
2138+
"items": {"type": "string"},
2139+
"strict": True
2140+
},
2141+
"contacts": {
2142+
"type": "array",
2143+
"items": {
2144+
"oneOf": [
2145+
{"type": "string"},
2146+
{
2147+
"type": "array",
2148+
"items": {
2149+
"type": "object",
2150+
"strict": True,
2151+
"properties": {
2152+
"value": {"type": "string"}
2153+
},
2154+
"required": ["value"]
2155+
}
2156+
}
2157+
],
2158+
"strict": True
2159+
}
2160+
}
2161+
}
2162+
}
2163+
original_schema = copy.deepcopy(schema)
2164+
cleaned = _remove_strict_from_schema(schema)
2165+
assert "strict" not in json.dumps(cleaned)
2166+
# schema should be unchanged, (should copy instead of mutate)
2167+
# otherwise it breaks cache keys
2168+
# https://github.com/BerriAI/litellm/issues/9692
2169+
assert cleaned != original_schema
2170+
assert schema == original_schema
2171+
2172+

0 commit comments

Comments
 (0)