Skip to content

fix(py): log a failed generate turn as one line without a traceback - #65

Merged
huangjeff5 merged 4 commits into
mainfrom
fix/py-log-failed-turn-one-line
Nov 11, 2026
Merged

huangjeff5 merged 4 commits into
mainfrom
fix/py-log-failed-turn-one-line

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

When a model or tool raises mid-generate, Genkit now logs a single ERROR line naming the culprit, innermost frame location, error, and trace_id instead of pretty-printing a multi-hundred-line rich traceback with frame locals.

# 1. A model or tool raises an unexpected exception mid-turn
response = await ai.generate(
    model='googleai/gemini-flash-latest',
    prompt='Validate order #4821 for peanut allergies.',
    tools=[check_allergens],
)
# => response.finish_reason == FinishReason.FAILED
# => response.error.message == 'internal error'

# --- BEFORE (stdout at default GENKIT_LOG=info) ---
# [error    ] tool 'check_allergens' raised, returning finish_reason=failed tool=check_allergens
# ╭───────────────────── Traceback (most recent call last) ──────────────────────╮
# │ ... ~1,000 lines of rich frames + local variables including prompt text ...  │
# ╰──────────────────────────────────────────────────────────────────────────────╯

# --- AFTER (stdout at default GENKIT_LOG=info) ---
# [error    ] tool 'check_allergens' raised, returning finish_reason=failed at=/app/tools.py:42 error='RuntimeError: allergen db down' tool=check_allergens trace_id=4bf92f3577b34da6a3ce929d0e0e4736

Changes

  • One-line ERROR record without exc_info by default: Previously, log_dropped_failure passed exc_info=exc at ERROR, so structlog's ConsoleRenderer (rich) formatted ~1,000 lines per failed turn, spent hundreds of ms of synchronous CPU on the event loop, and dumped frame locals (including prompt text and config) to stdout. The ERROR record now carries error='<Type>: <message>' (or '<Type>' when the message is empty) and at='<file>:<line>' for the innermost frame instead of exc_info. The caller already gets finish_reason='failed', and trace_id links to the span tree.
  • Full stack trace preserved under GENKIT_LOG=debug: ai.generate() boxes unexpected exceptions behind 'internal error' and spans do not store Python stack frames, so dropping exc_info everywhere would leave SDK or plugin bugs with no stack trace. Genkit also emits logger.debug('%s failure details', culprit, exc_info=exc, **fields), which structlog filters out at no rendering cost under default info logging and prints when GENKIT_LOG=debug is set.
  • Public errors stay unlogged: PublicError (and GenkitError without a foreign cause) already surfaces its message on response.error.message, so log_dropped_failure still skips it, and each boxed internal failure logs once at the originating tool or model.

Breaking changes

None.

@huangjeff5 huangjeff5 changed the title fix(py): log a failed generate turn as one line, without a traceback fix(py): log a failed generate turn as one line without a traceback Nov 10, 2026
@huangjeff5
huangjeff5 requested a review from hilariie November 10, 2026 05:10
The ERROR line no longer carries exc_info, and no span keeps the stack,
so a bug in Genkit or a plugin boxed as 'internal error' left no
traceback anywhere. Log it at debug, same as reflection and startup
failures.
@huangjeff5
huangjeff5 force-pushed the fix/py-log-failed-turn-one-line branch from ae70352 to 60eb727 Compare November 10, 2026 22:14

@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 #108 November 11, 2026 21:34
@huangjeff5
huangjeff5 merged commit 70a05e7 into main Nov 11, 2026
27 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-log-failed-turn-one-line branch November 11, 2026 22:39
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