fix(tracing): reconstruct flattened LLM tool_calls as a list#1771
Draft
cotovanu-cristian wants to merge 1 commit into
Draft
fix(tracing): reconstruct flattened LLM tool_calls as a list#1771cotovanu-cristian wants to merge 1 commit into
cotovanu-cristian wants to merge 1 commit into
Conversation
_get_llm_messages rebuilds nested structures from flattened OpenInference
attributes, but its container-type heuristic looked two segments ahead
(parts[i+2]) to decide list-vs-dict. For the key shape
llm.output_messages.0.message.tool_calls.0.tool_call.function.name the index
after tool_calls is followed by a non-digit ("tool_call"), so tool_calls was
built as a {"0": ...} dict. The toolCalls mapping iterates the value as a
list, so every tool call was silently dropped from the exported span.
Decide each child container's type from the next segment instead: a following
digit means the child is a list (indexed by int), otherwise a dict. This
rebuilds tool_calls — and any other list-valued message field — as a list.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
_get_llm_messagesreconstructs nested Python structures from flattened OpenInference spanattributes. Its list-vs-dict heuristic decided a segment was a list index only when the segment
two positions ahead was also a digit (
parts[i+2].isdigit()). For the standard tool-call keyshape:
the index after
tool_calls(0) is followed bytool_call(non-digit), so0stayed a stringkey and
tool_callswas built as{"0": {...}}— a dict. The consumer(
_map_llm_call_attributes) iteratesmessage.tool_callsas a list, so it found nothing andevery tool call was dropped from the exported span.
The contract
_map_llm_call_attributesreadsmessage.tool_callsas a list of{id, name, arguments}, and theLLMOps span schema expects
toolCallsto be a list. Per OpenInference flattened-attributesemantics, a digit segment denotes a list index. So a list-valued message field (notably
tool_calls) must be reconstructed as a list.Root cause & fix
tracing/_otel_exporters.py::_get_llm_messages— decide each child container's type from thenext segment, not two ahead: a following digit means the child is a list (indexed by
int,grown with
Noneplaceholders), otherwise a dict; a digit segment indexes into a list parent. Thisrebuilds
tool_calls— and any other list-valued field likemessage.contents— as a list. Thefix lands once in the shared producer; both input and output message paths are fixed together. The
list-iterating consumer already skips non-dict entries, so placeholder growth is safe.
Tests
Added
test_llm_span_tool_calls_reconstructed_as_list, which feeds the real flattened key shapefor an LLM span emitting two tool calls and asserts
message.tool_callsis a list andtoolCalls == [{id, name, arguments}, …]in order. It fails on current code (the value is a dict)and passes with the fix. Full
tests/tracingsuite: 27 passed;ruff,mypy, and thelint_httpx_client.py(UIPATH001) check all pass.