Skip to content

fix(py): show error message and traceback on failed spans in the Dev UI - #113

Merged
huangjeff5 merged 6 commits into
mainfrom
fix/py-devui-span-exception-events
Nov 12, 2026
Merged

huangjeff5 merged 6 commits into
mainfrom
fix/py-devui-span-exception-events

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

A failed flow or tool span in the Dev UI shows the error message as its heading and the Python traceback underneath again. Since 0.13.0 the heading read Error and the stack box was empty (or, with #63, a copy of the message).

Stacked on #63 (fix/py-trace-failed-interrupted-spans); will retarget to main after #63 merges.

# 1. A tool fails and the flow lets it escape
@ai.tool()
async def check_allergens(dish: str) -> str:
    raise ValueError('allergen db unavailable')

@ai.flow()
async def take_order(dish: str) -> str:
    return await check_allergens(dish)

# 2. Run it from the Dev UI, open the trace, select take_order or check_allergens
# before (0.13.0): heading "Error", no traceback
# after:           heading "allergen db unavailable", traceback ending in
#                  ValueError: allergen db unavailable

Checked in the Dev UI from genkit start (CLI 1.40.1): on this branch the take_order span details show allergen db unavailable as the heading and the full Python traceback below it. On the base branch the same trace shows Error with only the message in the box.

Why it broke

#6132 replaced the OTel SDK + TraceServerExporter with DirectHttpInstrumentation, which posted spans as OTLP/JSON to ${server}/api/otlp with events: []. Two problems stacked:

  • Python stopped recording the exception (0.12 called span.record_exception(e)).
  • The telemetry server's OTLP converter (telemetry-server/src/utils/otlp.ts toSpanData) drops span events and links anyway, so nothing sent on /api/otlp can reach timeEvents.

The Dev UI reads a non-model span's heading only from the first timeEvents.timeEvent with description: 'exception' (exception.message), and the stack box from exception.stacktrace, falling back to status.message.

Changes

  • Spans POST TraceData to /api/traces, logs stay on /api/otlp: CollectorHttpSink now encodes each export as the collector's TraceData/SpanData schema (ms timestamps, flat attribute map, instrumentationLibrary: genkit-tracer/v1, spanKind: INTERNAL, status: {code, message?}, timeEvents.timeEvent[]), one payload per span. This is the endpoint and shape JS and Go post, and what Python sent before 0.13, so the server's span merge and live-view broadcast treat Python spans the same way. The background worker, queue, flush timeout, shutdown, and URL scheme check are unchanged. _log_exporter.py is untouched.
  • A raised error records an exception event: run_in_new_span stores exception.type, exception.message and exception.stacktrace in the format OTel's Span.record_exception writes (ValueError, genkit._core._error.GenkitError, ''.join(traceback.format_exception(...))), as plain strings on ActiveSpan, so a finished span keeps no frames alive. _StreamingCallbackError records the callback's own error. An empty str(exc) leaves exception.message out, so the heading falls back to Error instead of going blank.

Decisions

  • exception.message is str(err), not GenkitError.message: For a GenkitError that's UNAVAILABLE: booking service down: after 5s, the same text as status.message and the last line of the traceback. .message would drop the status, which tells you whose fault it is, and the cause suffix, which GenkitError.__init__ builds specifically so the Dev UI shows the real reason. genkit:error keeps the bare .message as before. JS and Go also put the status-prefixed string here.
  • An interrupt records no exception event: The Dev UI titles a span Interrupted only when exception.message is missing, and Python's Interrupt stringifies its metadata ({'reason': 'manager approval'}), which would become the heading. The tool span keeps genkit:state: error + genkit:metadata:interrupt from fix(py): Dev UI traces show why generate failed, where it started, and interrupted tools #63, so it still renders as interrupted.
  • A failure returned without raising gets status, not an event: generate and prompt box failures into the response and call set_state('error', message=...). Nothing was raised on that span, so it gets status: {code: 2, message} and no exception event; the stack box shows the message through its status.message fallback. JS and Go don't synthesize an event there either, and 0.12 showed Error for these spans too, so this isn't a regression.
  • Start export stays unconditional, merge is the server's job: Each span still posts twice, once on start with endTime: 0 and no trace endTime, once on end. /api/traces already ignores a stale start for a span it has as ended (file store merge + spanBroadcastEvents), which /api/otlp didn't do for the live view. Only the root span sets the trace's displayName/startTime, and endTime once the root ends.
  • resource_attributes removed from the private sink/poster: TraceData has no resource block, so DirectHttpInstrumentation(sink) and export_spans(spans) drop the argument. Both are private (genkit._core._telemetry._http); only test sinks change.

Cross-SDK

Python's Dev UI span wire format changes from OTLP/JSON on /api/otlp to TraceData on /api/traces, the format JS and Go already send and every shipped telemetry server accepts. Failed span, before vs after:

// before: POST /api/otlp
{"resourceSpans": [{"scopeSpans": [{"spans": [{
  "name": "check_allergens", "events": [],
  "status": {"code": 2, "message": "allergen db unavailable"}, "...": "..."}]}]}]}

// after: POST /api/traces
{"traceId": "89a9…", "spans": {"8ccb…": {
  "displayName": "check_allergens", "startTime": 1794458298006.4, "endTime": 1794458298013.9,
  "status": {"code": 2, "message": "allergen db unavailable"},
  "timeEvents": {"timeEvent": [{"time": 1794458298013.9, "annotation": {
    "description": "exception",
    "attributes": {"exception.type": "ValueError", "exception.message": "allergen db unavailable",
                   "exception.stacktrace": "Traceback (most recent call last): …"}}}]},
  "...": "..."}}}

JS, Go and Dart owners: nothing to do.

Breaking changes

None. No public API changes.

Testing

  • trace_data_encoder_test.py (new): success span shape and ms timestamps; start export (endTime: 0, no trace endTime) then end export; parent/child ids and root-only trace fields; attribute types (bool/int/float/str, JSON input, NaN); failed span with type/message/stacktrace; error escaping tool → flow; GenkitError message; empty message; stream-callback unwrap; no frames held; interrupted tool renders Interrupted; boxed generate failure has status but no event. A small helper mirrors the Dev UI heading rule so the tests pin what the user sees.
  • telemetry_test.py: against a local HTTP collector, spans POST to /api/traces with the exception event and a get_logger line still POSTs to /api/otlp. Existing OTLP span assertions moved to TraceData.
  • End to end with genkit start (CLI 1.40.1) and a scratch app: ran take_order (tool raises ValueError('allergen db unavailable')) and comp_dessert (tool raises Interrupt) through the reflection server, then GET /api/traces/<id>. take_order and check_allergens both stored timeEvents with exception.type: ValueError, exception.message: allergen db unavailable, and a traceback ending in the tool's raise; approve_comp stored genkit:state: error, status: {code: 2} and no exception event; generate stayed success.
  • Preflight: ruff format/check (preview), check_consistency, check_license, liccheck, pyright, pyrefly, ty, full pytest.

Release note

Python: failed flow and tool spans in the Dev UI show the error message and Python traceback again (regressed in 0.13.0).

@huangjeff5
huangjeff5 added this pull request to stack #79 November 12, 2026 04:50
Base automatically changed from fix/py-trace-failed-interrupted-spans to main November 12, 2026 05:07
Since 0.13.0 the Dev UI poster sent spans as OTLP/JSON to /api/otlp with
no events. The telemetry server's OTLP converter drops span events anyway,
and the Traces tab reads a non-model span's heading and stack only from its
exception event, so failed Python flow and tool spans showed "Error" and no
traceback.

Spans now POST TraceData to /api/traces, the shape JS and Go send and what
Python sent before 0.13. A raised error records exception.type, .message
(str(err)) and .stacktrace the way OTel's record_exception formats them,
stored as strings so finished spans hold no frames. Interrupts record no
event so the tool span still renders as Interrupted. Failures returned
without raising keep status but get no event. Logs stay on /api/otlp.
@huangjeff5
huangjeff5 merged commit e92cc52 into main Nov 12, 2026
20 of 21 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-devui-span-exception-events branch November 12, 2026 23:36
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