Commit af2f027
fix: harden the max_tokens resolution — four regressions found by red-teaming
Adversarially probing #26 against the pre-change tree (275646b) turned up
four behaviour regressions and one dead branch. Every row below is measured,
not reasoned.
1. A NUMERIC CEILING WAS SILENTLY DELETED. `_positive_int` rejected anything
that was not an `int`, and the "strip an unusable value from a rejected
field" clause then removed it. So on Azure 2025:
max_tokens=50.0 before {'max_tokens': 50.0} after {} <- unbounded
max_tokens='50' before {'max_tokens': '50'} after {} <- unbounded
max_tokens=0 before {'max_tokens': 0} after {}
max_tokens=True before {'max_tokens': True} after {}
Floats genuinely reach litellm — `AnthropicConfig.map_openai_params` coerces
one with `max(1, int(round(value)))`, and `anthropic max_tokens=50.0` really
does go out as 50. Turning a caller's 50.0 into no ceiling at all is worse
than the defect #26 fixed.
Fixed two ways. Numeric values are now usable and coerced the same way
Anthropic already coerces them (NaN and inf rejected). And rule 3 no longer
strips anything: when no usable value can be derived, NOTHING is changed and
the provider rejects the request as loudly as it always did. Garbage in,
error out — that clause bought nothing (Azure rejects `max_tokens: 0` on its
own merits either way) and cost a silent unbounding.
2. IT RESURRECTED A DROPPED PARAM. A field in `additional_drop_params` was
still eligible as a target, so `drop: [max_completion_tokens]` plus a client
`max_tokens` went out AS `max_completion_tokens` — defeating an explicit
operator instruction:
drop=[max_completion_tokens], MT=50
before {'max_tokens': 50} after {'max_completion_tokens': 50}
The drop list is now consulted for every target choice, including the
directive's. If both fields are dropped or unsupported, nothing happens.
3. A NON-STRING api_version RAISED A NEW EXCEPTION TYPE. The v1-api check does
a set membership test, so a list or dict `api_version` raised
`unhashable type` from param mapping where the pre-change tree raised
`AttributeError` from its own `.split()`. Now anything that is not a `str`
yields "no preference" — this lookup runs on every Azure chat request and
must not invent a failure mode. The remaining int/bytes/list crashes are
litellm's own pre-existing `api_version.split("-")` and are now byte-for-byte
identical to pre-change.
4. THE PREFERENCE RESOLVED AGAINST A DIFFERENT api_version THAN THE REQUEST
USED. The fallback to `litellm.api_version` / `AZURE_API_VERSION` /
`AZURE_DEFAULT_API_VERSION` triggered on `is None`, while the azure branch
below it uses `or`-falsiness. An `api_version=""` therefore resolved the
preference against nothing while the request went out on the 2025 default —
`max_tokens` to a version that rejects it. Now matches the same falsiness.
DEAD BRANCH REMOVED. The `extra_body` handling could never fire: `extra_body`
is not a mappable chat param, so it never appears in `non_default_params`.
Verified — with `extra_body={'max_completion_tokens': 64000}` neither the strip
nor the min() saw it. Code that claims a guarantee it does not provide is worse
than no code, so it is gone and the limitation is documented instead: a caller
who reaches for `extra_body` is explicitly bypassing param mapping, and the h2o
cap hook is where that channel is policed.
ALSO VERIFIED, NO CHANGE NEEDED. `azure_text` (the legacy /completions
endpoint, which has no `max_completion_tokens` at all) resolves to a different
provider config and is untouched — the same class of hazard that made the
earlier deployment-hook approach break /v1/messages. Now covered by a test.
Live re-verification through a litellm proxy against a capture server, client
sending max_tokens=50:
azure 2025, mt=50 -> max_completion_tokens: 50
azure 2025, mt=50.0 -> max_completion_tokens: 50
azure 2025, drop=[MCT] -> max_tokens: 50
azure 2025, directive=false -> max_tokens: 50
anthropic ceiling 64000 -> max_tokens: 50
86 tests, up from 60. The float, drop-list, api_version-type and
leave-garbage-alone cases all fail on #26's head.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 60f5ee9 commit af2f027
4 files changed
Lines changed: 252 additions & 57 deletions
File tree
- litellm
- litellm_core_utils
- llms/azure/chat
- tests/test_litellm/litellm_core_utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
46 | 67 | | |
47 | 68 | | |
48 | | - | |
| 69 | + | |
49 | 70 | | |
50 | 71 | | |
51 | 72 | | |
| |||
59 | 80 | | |
60 | 81 | | |
61 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
62 | 86 | | |
63 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
64 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
65 | 117 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
69 | 121 | | |
70 | 122 | | |
71 | 123 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
80 | 134 | | |
81 | 135 | | |
82 | 136 | | |
83 | 137 | | |
84 | 138 | | |
85 | 139 | | |
| 140 | + | |
86 | 141 | | |
87 | 142 | | |
88 | 143 | | |
| |||
94 | 149 | | |
95 | 150 | | |
96 | 151 | | |
| 152 | + | |
| 153 | + | |
97 | 154 | | |
98 | 155 | | |
99 | 156 | | |
| |||
104 | 161 | | |
105 | 162 | | |
106 | 163 | | |
107 | | - | |
| 164 | + | |
108 | 165 | | |
109 | 166 | | |
110 | 167 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 168 | + | |
| 169 | + | |
119 | 170 | | |
120 | 171 | | |
121 | 172 | | |
122 | 173 | | |
123 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
124 | 177 | | |
125 | 178 | | |
126 | | - | |
| 179 | + | |
| 180 | + | |
127 | 181 | | |
128 | 182 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
139 | 196 | | |
140 | 197 | | |
141 | 198 | | |
142 | 199 | | |
143 | 200 | | |
144 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
164 | 167 | | |
165 | | - | |
| 168 | + | |
166 | 169 | | |
167 | 170 | | |
168 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3883 | 3883 | | |
3884 | 3884 | | |
3885 | 3885 | | |
3886 | | - | |
3887 | | - | |
3888 | | - | |
3889 | | - | |
| 3886 | + | |
| 3887 | + | |
| 3888 | + | |
| 3889 | + | |
| 3890 | + | |
3890 | 3891 | | |
3891 | 3892 | | |
3892 | 3893 | | |
| |||
3903 | 3904 | | |
3904 | 3905 | | |
3905 | 3906 | | |
| 3907 | + | |
3906 | 3908 | | |
3907 | 3909 | | |
3908 | 3910 | | |
| |||
0 commit comments