Skip to content

fix(py/agents): redact custom agent exception text in failed turns - #7112

Closed
huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-custom-agent-redact-errors
Closed

huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-custom-agent-redact-errors

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

A custom agent that raises a plain exception now records INTERNAL / internal error on the failed turn, not str(exc). The original exception goes to the server log with its traceback. This is the same rule generate uses for a raising tool. AgentOutput.error and snapshot errors reach HTTP callers in a 200 response body, so before this change any exception text, including credentials and DSNs, went straight to the client.

# 1. A turn handler hits a driver error
async def handle_turn(inp, ctx):
    raise ValueError('db password=hunter2')

app.include_router(serve_agent(orders_agent))

# --- BEFORE ---
# POST /ordersDb (with or without GENKIT_ENV=dev)
# => 200 {"finishReason": "failed",
#         "error": {"status": "INTERNAL", "message": "db password=hunter2"}}

# --- AFTER ---
# POST /ordersDb
# => 200 {"finishReason": "failed",
#         "error": {"status": "INTERNAL", "message": "internal error"}}
# server log:
# => agent 'ordersDb' raised, returning finish_reason=failed  agent=ordersDb
#    Traceback ... ValueError: db password=hunter2

# 2. An intentional, caller-facing failure still passes through
raise GenkitError(status='NOT_FOUND', message='no order A-17')
# => {"status": "NOT_FOUND", "message": "no order A-17"}

Main Changes

  • Foreign exception text stays server-side: Previously, to_error_details used str(exc) for anything that wasn't a GenkitError. Prompt agents didn't leak, because generate boxes tool and model errors first. Custom agents had no such step. Non-Genkit exceptions now become internal error, using generate's INTERNAL_FINISH_MESSAGE. Status classification is unchanged: TimeoutError maps to DEADLINE_EXCEEDED and CancelledError to CANCELLED.
  • GenkitError follows the tool-error rule exactly: to_error_details now calls generate's boxed_finish_message. A GenkitError or PublicError keeps its status and message. An INTERNAL GenkitError with a foreign cause= is redacted, because its message is usually the wrapped text. That matches what a flow does when it wraps a RuntimeError (see exp_agents_import_test). One exception: interrupt_escape_error's "Interrupt raised outside a tool" keeps its framework-authored message, since the attached cause there is an Interrupt, not foreign text.
  • Logged once, named by agent: Hidden causes go through generate's log_dropped_failure, which gets a new agent= field so the log line names the culprit. A prompt agent whose tool crashes is still logged only once. generate already logged the tool error, and the runtime sees a GenkitError that already carries internal error, so it has nothing new to hide.

Parity

  • Go (go/ai/exp/agent.go clientSafeError): the store keeps the full text. When the agent is served, AgentOutput.Error and snapshot errors are rewritten to the generic status message unless the error is PublicErrorf or the env is dev. Python now redacts at record time, so the stored snapshot is redacted too. Python has no "served" context marker, and a snapshot read back over getSnapshot is client-visible either way.
  • JS (js/ai/src/agent.ts toErrorDetails): still records e.message unredacted. Same bug, follow-up PR.

Tests

  • custom agent raises ValueError → INTERNAL / internal error, secret absent from the wire shape, one ERROR log naming the agent with the original exception.
  • custom agent raises GenkitError(NOT_FOUND, ...) → passes through, no log.
  • prompt agent with crashing tool → internal error, logged once (tool site only).
  • to_error_details parametrized: adds PublicError passthrough and the redacted INTERNAL wrapper with a foreign cause.
  • The conformance fixture customAgentFailing now raises GenkitError('intentional failure'), because the shared spec reads that message back off the snapshot. The detach test now asserts the redacted snapshot.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the Genkit Python SDK's agent runtime to redact foreign exception messages, replacing them with a generic 'internal error' to prevent raw exception details from leaking to clients. The original tracebacks are instead logged on the server side using log_dropped_failure. Feedback on the changes suggests avoiding logging asyncio.CancelledError as an error to prevent polluting production logs during normal client cancellations.

Comment on lines +1223 to +1224
message = INTERNAL_FINISH_MESSAGE
redacted = True

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.

medium

Logging asyncio.CancelledError as an ERROR with a traceback on every normal cancellation or client disconnect can severely pollute production logs and trigger false alarms in error monitoring systems. Since cancellation is a normal lifecycle event and does not represent a leaked failure cause, we should avoid calling log_dropped_failure for it by setting redacted to False when the exception is a CancelledError.

Suggested change
message = INTERNAL_FINISH_MESSAGE
redacted = True
message = INTERNAL_FINISH_MESSAGE
redacted = not isinstance(exc, asyncio.CancelledError)

@huangjeff5

Copy link
Copy Markdown
Contributor Author

Closing: migrated to standalone repository genkit-ai/genkit-python (PR #41): genkit-ai/genkit-python#41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant