Repository navigation
fix(py/otel): set span error status on failed turns and add flushable lifecycle helpers - #55
Merged
huangjeff5 merged 9 commits intoNov 11, 2026
Conversation
huangjeff5
force-pushed
the
fix/py-otel-bugbash-followups
branch
from
November 9, 2026 19:08
65e7961 to
53a1342
Compare
huangjeff5
force-pushed
the
fix/py-otel-bugbash-followups
branch
from
November 10, 2026 07:44
53a1342 to
5c11ba6
Compare
cabljac
approved these changes
Nov 10, 2026
huangjeff5
force-pushed
the
fix/py-otel-bugbash-followups
branch
from
November 10, 2026 19:57
5c11ba6 to
5a57b6b
Compare
huangjeff5
force-pushed
the
fix/py-otel-bugbash-followups
branch
from
November 11, 2026 06:23
ad659fc to
9d8fc7a
Compare
The operation.details event path called bare json.dumps on mapped messages. part_json dumps in python mode, so a datetime in a tool response raised TypeError after the model had already replied, and generate returned finish_reason=failed. Route span and event content through one non-raising encoder; datetime/UUID/Decimal now encode via pydantic_core.to_jsonable_python (ISO 8601 for datetime). (BR-02)
…tion flushable set_custom_metadata_attributes, set_span_state, SpanState, flush_instrumentations, and dispose_instrumentations lived only in genkit._core, so apps stamping tenant_id or draining spans on serverless shutdown needed private imports. Export them from genkit.telemetry. GenAiInstrumentation now implements FlushableInstrumentation: flush() force-flushes the global OTel tracer, meter, and logger providers, skipping proxies and logging (not raising) on flush failure. (BR-03)
A model can return ModelResponse(finish_reason=failed, error=...) instead of raising. _run_model_span ignored response.error and _resolve_finish_reasons hardcoded failed=False, so the chat span stayed UNSET with finish_reasons=['stop']. Now the span gets StatusCode.ERROR, error.type (the Genkit status, or _OTHER), finish_reasons=['error'], and the duration metric carries error.type. (BR-04)
…ttributes set_span_state and SpanState write Genkit's own genkit:state, and dispose_instrumentations already runs at exit. Keep them internal so the public surface is the two calls app code actually makes. Document the flush in the genkit-otel README.
flush_instrumentations runs on every Dev UI runAction success. With GenAiInstrumentation flushable, that forced a metric and log export and waited on all three OTel providers per run, up to the force_flush timeout each when the collector is down. The reflection server now calls an internal flush_span_exports(), which uses a provider's private _flush_spans hook when present (GenAiInstrumentation flushes its tracer provider only) and falls back to flush(). Same scope as JS flushTracing. The public flush_instrumentations() still drains all three for serverless shutdown.
…data/bytes encoding
huangjeff5
force-pushed
the
fix/py-otel-bugbash-followups
branch
from
November 11, 2026 14:37
9d8fc7a to
a9eee50
Compare
huangjeff5
enabled stacked auto-merge (squash)
November 11, 2026 14:38
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.
OpenTelemetry chat spans now go ERROR when a model returns its failure instead of raising. EVENT_ONLY content capture no longer fails the model call on non-JSON values.
genkit.telemetryexportsflush_instrumentations()so a serverless handler can drain exports before the process freezes.Changes
_run_model_spanignoredresponse.errorand_resolve_finish_reasonshardcodedfailed=False. So a model that returnedModelResponse(finish_reason=failed, error=...)left the chat span UNSET withfinish_reasons=['stop']. The chat span now getsStatusCode.ERRORwith the failure message,error.typeset to the Genkit status (or_OTHERwhen there isn't one), andfinish_reasons=['error'].gen_ai.client.operation.durationgains the sameerror.typeseries. A raised exception keeps the existingrecord_exceptionpath. Thegeneratespan was already ERROR on any boxed failure.json.dumpson mapped messages. A datetime in a tool response raisedTypeErrorafter the model had already replied, and generate returnedfinish_reason=failed. Span, event, and custom metadata values now go through one non-raising encoder (to_jsonable_pythonwithby_alias=True, exclude_none=True, bytes_mode='base64', fallback=str).genkit.telemetry:FlushableInstrumentationwas public, butflush_instrumentationslived ingenkit._core, so a serverless drain needed a private import.genkit.telemetrynow exportsflush_instrumentationsandset_custom_metadata_attributes(stamptenant_idon the active span).set_span_stateanddispose_instrumentationsstay internal: Genkit writesgenkit:stateitself, and dispose already runs at exit.GenAiInstrumentationis flushable:flush()force-flushes the global OTel tracer, meter, and logger providers. It skips proxy providers and logs instead of raising if a flush fails. Providers behind an injectedtracer,meter, orotel_loggeraren't reachable from there; flush those on the provider you built.genkit start, the reflection server flushes before everyrunActionresponse so the trace is ready when the UI asks for it. It used to callflush_instrumentations(), which also blocked each click on remote OTel / APM exports. It now flushes only the Dev UI collector (flush_developer_ui_collector).Breaking changes
None.