Commit dc40aec
authored
fix(instrumentation): all 4 usage sources are 'if' not 'elif' (#59)
* fix(instrumentation): all 4 usage sources are 'if' not 'elif'
The pre-fix `extract_usage_from_response` walked the 4
source branches as an elif chain:
```
if hasattr(response, 'usage_metadata'): # empty dict
elif hasattr(response, 'generations'): # LLMResult
elif hasattr(response, 'usage'):
elif hasattr(response, 'response_metadata'): # real tokens
```
A LangChain AIMessage can carry token info on multiple
attributes at once. When the first branch's `hasattr`
returns True but the value is an empty / 0/0/0 dict
(LangChain-internal init state for streaming, callbacks,
some provider wrappers), every subsequent `elif` was
skipped — including the one carrying the real token
counts.
Reproducer (covered by the new test):
```
response.usage_metadata = {input_tokens: 0, output_tokens: 0, total_tokens: 0}
response.response_metadata = {token_usage: {prompt_tokens: 26, completion_tokens: 48, total_tokens: 74}}
# Pre-fix: ships tokens=0 to /track. LLM call invisible on dashboard.
# Post-fix: ships tokens=74, input_tokens=26, output_tokens=48.
```
Switched all 4 source branches to plain `if` so each one
attempts its read; later branches naturally overwrite the
zero default when the earlier branch's value is empty. In
practice LangChain providers never put conflicting token
counts on two attributes of the same response, so
last-wins is safe; the docstring on the function is
updated to spell that out.
Added `test_extract_usage_metadata_zero_response_metadata_real`
that pins the regression: empty usage_metadata + populated
response_metadata must surface the real numbers.
All 39 tests in test_langgraph_callback.py still pass;
test_extractors.py + test_instrumentation_phase41.py also
green (no regression in the wire-shape normalization
testers).
* chore(release): bump version 0.13.3 -> 0.13.4
Pairs with 4840a33 (fix-instrumentation-langchain-elif-chain).
Wire format unchanged; pure version bump + changelog entry
so the SDK_MIN_VERSION floor is up to date with the bug fix.
---------1 parent 2043e09 commit dc40aec
3 files changed
Lines changed: 72 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
279 | 303 | | |
280 | 304 | | |
281 | | - | |
| 305 | + | |
282 | 306 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
214 | | - | |
| 214 | + | |
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | | - | |
| 236 | + | |
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
254 | 266 | | |
255 | | - | |
| 267 | + | |
256 | 268 | | |
257 | 269 | | |
258 | 270 | | |
| |||
269 | 281 | | |
270 | 282 | | |
271 | 283 | | |
272 | | - | |
| 284 | + | |
273 | 285 | | |
274 | 286 | | |
275 | 287 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
49 | 80 | | |
50 | 81 | | |
51 | 82 | | |
| |||
0 commit comments