Skip to content

fix(py): give each tool-loop turn its own generate span in traces - #64

Merged
huangjeff5 merged 3 commits into
mainfrom
fix/py-trace-generate-per-turn-spans
Nov 10, 2026
Merged

huangjeff5 merged 3 commits into
mainfrom
fix/py-trace-generate-per-turn-spans

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Each tool-loop turn after the first now gets its own generate span, nested under the turn before it. Before, every turn's model call sat flat under the one outer generate, so a 3-turn call showed three model spans side by side and a failure on turn 3 painted only the outer span red.

response = await ai.generate(prompt='weather in Paris', tools=[get_weather])

# before
# generate
# ├ my-model      (turn 1)
# ├ get_weather
# └ my-model      (turn 2)

# after
# generate
# ├ my-model      (turn 1)
# ├ get_weather
# └ generate      (turn 2)
#   └ my-model

Changes

  • One generate span per follow-up turn: generate_turn runs the next turn inside run_in_new_span('generate', ..., action_type='util'). Turn n+1's wrap_generate middleware, model call, and tools all sit inside it. Turn 3 nests under turn 2, and so on.
  • Per-turn input and output: each turn span's input is the conversation sent that turn; its output is that turn's response.
  • Failure lands on the turn: when a turn comes back failed, its own span and every span above it are marked error (and stacked PR fix(py): Dev UI traces show why generate failed, where it started, and interrupted tools #63 writes the failure cause onto genkit:error and status.message for each failed generate span).
  • No runtime change: response.text, finish_reason, and messages are unchanged. With no instrumentation configured, the extra span is a no-op.

Breaking changes

None. The trace has more spans, nested. Return values and public APIs are unchanged.

Cross-SDK

Matches JS (generateHelper opens a generate span per turn before running wrapGenerate hooks and recurses):

  • Hook placement vs. Go: Go opens the turn span inside its WrapGenerate chain, whereas Python follows JS and opens the turn span outside wrap_generate so every turn is guaranteed a generate span even when a middleware short-circuits or raises before calling next(), and turn N has the same shape as turn 0 (the post-hook request is already recorded on the model span).
  • step_name: JS names the span from stepName; Python ignores step_name today and leaves that for a follow-up so this PR stays focused on per-turn span nesting.

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comments

@huangjeff5
huangjeff5 added this pull request to stack #79 November 10, 2026 19:38
@huangjeff5
huangjeff5 force-pushed the fix/py-trace-generate-per-turn-spans branch from 12539a8 to 1cb3887 Compare November 10, 2026 19:41
@huangjeff5
huangjeff5 enabled stacked auto-merge (squash) November 10, 2026 19:41
@huangjeff5
huangjeff5 merged commit e7ede40 into main Nov 10, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-trace-generate-per-turn-spans branch November 10, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants