Commit 0878e27
committed
fix(utils): keep token counts when merging batched embed responses
Client.embed() sends every call through merge_embed_responses unless you
pass batching=False or images, and that path rebuilds ApiMeta from
scratch in merge_meta_field. The rebuild only sets api_version, four of
the six ApiMetaBilledUnits fields, and warnings.
So meta.tokens, meta.cached_tokens, billed_units.images and
billed_units.image_tokens are discarded on the way out. The same request
returns different metadata depending on whether batching is on:
co.embed(texts=texts).meta.tokens # None
co.embed(texts=texts, batching=False).meta.tokens # populated
Anyone reading meta.tokens for usage accounting silently gets nothing.
Sum the missing fields the same way the existing four are summed.
cached_tokens sits directly on ApiMeta so it sums off the meta list
rather than the billed_units list. tokens stays None when no input meta
carried it, so a merged response with no token counts looks exactly like
it does today and the existing equality assertions still hold.
merge_meta_field names every field it copies by hand, which is why these
four went missing when they were added to the models. One of the tests
drives its assertions off the model fields instead of a fixed list, so
the next field added to ApiMeta fails there rather than silently
disappearing from merged responses.1 parent 050f9c0 commit 0878e27
2 files changed
Lines changed: 82 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
173 | 174 | | |
| 175 | + | |
174 | 176 | | |
175 | 177 | | |
176 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
177 | 191 | | |
178 | 192 | | |
179 | 193 | | |
180 | 194 | | |
| 195 | + | |
181 | 196 | | |
| 197 | + | |
182 | 198 | | |
183 | 199 | | |
184 | 200 | | |
185 | 201 | | |
| 202 | + | |
| 203 | + | |
186 | 204 | | |
187 | 205 | | |
188 | 206 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
205 | 206 | | |
206 | 207 | | |
207 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
208 | 269 | | |
209 | 270 | | |
210 | 271 | | |
| |||
0 commit comments