Skip to content

fix(py): Dev UI traces show why generate failed, where it started, and interrupted tools - #63

Merged
huangjeff5 merged 6 commits into
mainfrom
fix/py-trace-failed-interrupted-spans
Nov 12, 2026
Merged

huangjeff5 merged 6 commits into
mainfrom
fix/py-trace-failed-interrupted-spans

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

When a tool, model, or loop cap fails inside ai.generate(), the failed generate (and enclosing prompt) span in the Dev UI now shows the failure text instead of a blank red row, marks only the span where the failure started, and renders interrupted tools as interrupted instead of green successes.

# 1. Register a tool that fails mid-turn
@ai.tool()
async def check_allergens(dish: str) -> str:
    raise RuntimeError('allergen service offline')

# 2. Run generate inside a flow; generate returns FinishReason.FAILED
@ai.flow()
async def order_flow(dish: str) -> str:
    res = await ai.generate(prompt=f'Is {dish} safe?', tools=[check_allergens])
    res.error.message
    # => 'internal error'
    return res.text

# --- BEFORE (Dev UI trace) ---
# order_flow         success
#   generate         error    (no status message)
#     check_allergens error   'allergen service offline'

# --- AFTER (Dev UI trace) ---
# order_flow         success                              genkit:isRoot=true
#   generate         error    'allergen service offline'
#     check_allergens error   'allergen service offline'  genkit:isFailureSource=true

Changes

  • Failed generate and prompt spans carry the root-cause message: Previously, a generate or named prompt call returning finish_reason='failed' marked its span error with no text, leaving the trace row red and blank. Genkit now writes the unredacted message (e.g. 'allergen service offline') to the span's status. response.error.message stays 'internal error': traces are for the developer debugging the run, response.error is for the caller.
  • Only the originating span is marked genkit:isFailureSource: Previously, the Dev UI poster never set genkit:isFailureSource or genkit:isRoot, so the Traces tab could not point at where an error started. The first failing span now claims genkit:isFailureSource = true: the raising model / tool span (followed through __cause__), or the innermost generate turn when max_turns or tool lookup fails with no child span. Enclosing spans turn red without re-claiming it.
  • Only explicit causes keep a child as the failure source: A span is the failure source unless the exception it raised is, or explicitly chains via from / cause= to, one a child already claimed. Implicit __context__ (a new error raised inside except without from) no longer counts, matching JS cause and Go %w.
  • Interrupted tool spans render as interrupted, not green: Previously, a tool raising Interrupt closed its span as success, and a bare Interrupt() wrote no genkit:metadata:interrupt at all. The tool span now records genkit:state = 'error' (status.code = 2, no message) plus genkit:metadata:interrupt (true when bare, else the metadata JSON). The Dev UI renders that as "Interrupted"; parent generate and flow spans stay green.

API changes

  • genkit.telemetry.SpanContext.set_state accepts *, message: str | None = None: Genkit now calls span.set_state(state, message=...) so failed generate and prompt spans forward the failure text (DirectSpanContext writes genkit:error and status.message; GenAiSpanContext sets StatusCode.ERROR with the description). Today SpanContext is only implemented by core and genkit-otel (both updated here); any future custom SpanContext implementation must accept the message keyword argument.

Breaking changes

None.

Cross-SDK

Python now writes the existing Dev UI trace attributes shared with JS and Go: genkit:isRoot, genkit:isFailureSource, and genkit:metadata:interrupt. One difference: on a bare Interrupt(), JS omits genkit:metadata:interrupt and the Dev UI keys off a ToolInterruptError exception event. Python's poster does not emit exception events, so it writes genkit:metadata:interrupt = "true" instead and the Dev UI still renders "Interrupted". No changes needed in JS, Go, Dart, or genkit-tools.

@huangjeff5
huangjeff5 force-pushed the fix/py-trace-failed-interrupted-spans branch 2 times, most recently from 04080dd to 3be2f3d Compare November 10, 2026 07:32
@huangjeff5
huangjeff5 changed the base branch from main to fix/py-trace-generate-per-turn-spans November 10, 2026 07:32
@huangjeff5
huangjeff5 force-pushed the fix/py-trace-failed-interrupted-spans branch from 3be2f3d to 9e94739 Compare November 10, 2026 19:17
@huangjeff5
huangjeff5 added this pull request to stack #79 November 10, 2026 19:38
@huangjeff5
huangjeff5 force-pushed the fix/py-trace-failed-interrupted-spans branch from 9e94739 to f84f188 Compare November 10, 2026 19:41
Base automatically changed from fix/py-trace-generate-per-turn-spans to main November 10, 2026 19:49
@huangjeff5
huangjeff5 force-pushed the fix/py-trace-failed-interrupted-spans branch 3 times, most recently from 9a6c934 to 76b308b Compare November 10, 2026 23:07
@huangjeff5 huangjeff5 changed the title fix(py): Dev UI traces show why generate failed, where it started, and interrupted tools fix(py)!: Dev UI traces show why generate failed, where it started, and interrupted tools Nov 11, 2026
@huangjeff5
huangjeff5 force-pushed the fix/py-trace-failed-interrupted-spans branch from a6c87b3 to 9dd726c Compare November 11, 2026 17:57
@huangjeff5 huangjeff5 changed the title fix(py)!: Dev UI traces show why generate failed, where it started, and interrupted tools fix(py): Dev UI traces show why generate failed, where it started, and interrupted tools Nov 11, 2026
mark_failure_source now walks __cause__ only (raise ... from e,
GenkitError(cause=e)). A new error raised inside except without from
claims the parent span, same as JS cause and Go %w.
…nterrupted-spans

# Conflicts:
#	packages/genkit/src/genkit/_ai/_agents/_runtime.py
@huangjeff5
huangjeff5 merged commit b865e5a into main Nov 12, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-trace-failed-interrupted-spans branch November 12, 2026 05:07
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