Commit 113fac7
authored
fix(llmobs): persist annotation context across batches (#15571)
## Description
[**For this support card**
](https://datadoghq.atlassian.net/browse/MLOS-298)
Taking a look at the [with_structured_output
method](https://github.com/langchain-ai/langchain/blob/85012ae601a8ce2bd1ecfcdc889fb9151423ddaf/libs/partners/openai/langchain_openai/chat_models/azure.py#L833)
it calls RunnableSequence.batch(), instead of BaseChatModel.batch()
(what is called otherwise) which causes us to not reactivate the context
after the first llm batch call.
Essentially RunnableSequence.batch() creates a root-level span that
encompasses the entire operation. When this root span finishes, the
context provider's cleanup logic runs and (because _reactivate=False)
the annotation context is lost. Here’s a nice code flow cursor made for
me:
User calls: gpt_4.with_structured_output(Schema).batch([...])
↓
RunnableSequence.batch() - TRACED (creates chain span as ROOT)
↓
Internally calls ChatModel.generate() - creates child LLM spans
↓
Chain span finishes → triggers context provider's _update_active()
↓
Since _reactivate=False, annotation context is NOT restored
↓
Second batch() has no active context → annotations don't apply
What we can do is track the context differently and reactivate it for
batches, and then deactivate it when we know we’re actually leaving the
context block.
## Testing
Added tests to make sure we persist context across multiple roots
## Risks
<!-- Note any risks associated with this change, or "None" if no risks
-->
## Additional Notes
<!-- Any other information that would be helpful for reviewers -->1 parent e562d52 commit 113fac7
File tree
3 files changed
+57
-1
lines changed- ddtrace/llmobs
- releasenotes/notes
- tests/llmobs
3 files changed
+57
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1067 | 1067 | | |
1068 | 1068 | | |
1069 | 1069 | | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
1070 | 1073 | | |
1071 | 1074 | | |
1072 | 1075 | | |
1073 | 1076 | | |
1074 | 1077 | | |
1075 | 1078 | | |
| 1079 | + | |
| 1080 | + | |
1076 | 1081 | | |
1077 | 1082 | | |
| 1083 | + | |
1078 | 1084 | | |
| 1085 | + | |
1079 | 1086 | | |
1080 | 1087 | | |
1081 | 1088 | | |
| |||
1098 | 1105 | | |
1099 | 1106 | | |
1100 | 1107 | | |
1101 | | - | |
| 1108 | + | |
1102 | 1109 | | |
1103 | 1110 | | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
1104 | 1115 | | |
1105 | 1116 | | |
1106 | 1117 | | |
| |||
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1399 | 1399 | | |
1400 | 1400 | | |
1401 | 1401 | | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
1402 | 1440 | | |
1403 | 1441 | | |
1404 | 1442 | | |
| |||
0 commit comments