Skip to content

fix(py/agents): redact agent exception text on the wire - #41

Merged
huangjeff5 merged 9 commits into
mainfrom
fix/py-custom-agent-redact-errors
Nov 12, 2026
Merged

huangjeff5 merged 9 commits into
mainfrom
fix/py-custom-agent-redact-errors

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

A served agent whose turn raised sent the raw exception text to the HTTP caller. A failed turn goes out as a normal 200 result, so error_body, which redacts errors for served flows, never saw it. genkit.web now applies the same rule to a failed turn's error: only a PublicError's message leaves the process.

# 1. A custom agent turn hits an internal failure with sensitive text
async def handle_turn(inp, ctx):
    raise ValueError('db password=hunter2')

app.include_router(serve_agent(orders_agent))

# --- BEFORE ---
# POST /ordersDb
# => 200 {"result": {"finishReason": "failed", "error": {"status": "INTERNAL", "message": "db password=hunter2"}}}

# --- AFTER ---
# 2. Over HTTP, a non-public failure keeps its status and gets a generic message
# => 200 {"result": {"finishReason": "failed", "error": {"status": "INTERNAL", "message": "Internal Error"}}}

# 3. In process, callers and stores keep the real text
res = await orders_agent.chat().send('refund A-17')
print(res.error.message)
# => db password=hunter2

# 4. Raising PublicError sends the message (and optional reason) over HTTP
raise PublicError('PERMISSION_DENIED', 'table 12 is reserved')
# => 200 {"result": {"finishReason": "failed", "error": {"status": "PERMISSION_DENIED", "message": "table 12 is reserved"}}}

Main Changes

  • Redact at the HTTP boundary, not when recorded: result_json and to_sse pass AgentOutput.error and SessionSnapshot.error through client_safe_failure. status stays so clients can branch on it; a non-public message becomes generic status text (Internal Error, Not Found, ...) and details is dropped. FastAPI, Flask, Django, getSnapshot, and custom to_sse routes all frame through genkit.web. In-process callers (chat().send()), session stores, and the Dev UI keep the real text. Caller-facing snapshot and resume errors (SNAPSHOT_NOT_FOUND, SNAPSHOT_NOT_RESUMABLE, INVALID_RESUME, UNRESOLVED_TOOL_REQUEST, and snapshot lookup selector errors) now raise PublicError(..., reason=...) (matching Go's status.PublicErrorf), and PublicError gains a keyword-only reason: ErrorReason | None = None parameter.
  • PublicError provenance rides on Failure without changing equality: When a turn raises PublicError, to_error_details and generate set a private, non-serialized flag on Failure (stripping any stack key in details before the wire). InMemorySessionStore drops private Failure provenance on copy so stored snapshot errors are redacted on HTTP getSnapshot consistently with JSON-backed stores (FileSessionStore, FirestoreSessionStore) and Go's copySnapshot. Failure.__eq__ compares self.__dict__ == other.__dict__, ignoring private attributes.
  • Hidden causes are logged once on the server: Every non-PublicError turn failure (except an escaped Interrupt) logs one ERROR line with its traceback and agent=<name> so the server log retains what the HTTP wire redacts, skipping failures generate already logged for a crashing tool or model.

Breaking changes

  • Served agents now redact non-PublicError failure text and details on HTTP (status is kept); caller-facing snapshot/resume errors (SNAPSHOT_NOT_FOUND, SNAPSHOT_NOT_RESUMABLE, INVALID_RESUME, UNRESOLVED_TOOL_REQUEST) stay readable as PublicError with details.reason; PublicError gains a keyword-only reason= parameter; stored snapshot errors are always redacted on HTTP getSnapshot across all session stores.

Cross-SDK

On the HTTP agent wire (POST /<agent>, ?stream=true, and getSnapshot), non-public failures drop details and replace message with a generic status label:

// before
{"result":{"finishReason":"failed","error":{"status":"INTERNAL","message":"db password=hunter2","details":{"reason":"TOOL_FAILED"}}}}
// after
{"result":{"finishReason":"failed","error":{"status":"INTERNAL","message":"Internal Error"}}}
  • Go: nothing to do. Go already keeps the text in process and store (convertKeepText), marks caller-facing snapshot/resume errors with status.PublicErrorf, and redacts non-public and stored snapshot errors on the wire (clientSafeError, status.Error.Public).
  • JS / Dart: no schema change; AgentOutput and SessionSnapshot keep their shape. JS toErrorDetails still sends err.message unredacted on served agent responses.

@huangjeff5
huangjeff5 force-pushed the fix/py-custom-agent-redact-errors branch from 53b8846 to 1f6b023 Compare November 9, 2026 19:07
@huangjeff5 huangjeff5 changed the title fix(py/agents): redact custom agent exception text in failed turns fix(py/agents): redact agent exception text on the wire Nov 10, 2026
SessionRunner is exported from genkit.exp.agent, so a public agent_name
attribute shows up on every custom agent's runner. It only feeds the
redaction log line.
Recording 'internal error' in to_error_details lost the text for
in-process callers, the store, and the Dev UI, and still let any
non-public GenkitError (NOT_FOUND, provider_error 429) reach HTTP
callers verbatim.

Move redaction to genkit.web, where every adapter frames results:
result_json and to_sse pass a failed AgentOutput/SessionSnapshot error
through client_safe_failure. A PublicError's Failure is marked (private,
unserialized flag, like Go's status.Error.Public) and goes out as is;
anything else keeps its status with a generic message and no details.
Same rule as error_body for flows and Go's clientSafeError.

to_error_details keeps the exception text again, matching JS and Go, so
customAgentFailing goes back to a plain RuntimeError and the shared
agent.yaml case runs on real behavior. The server-side log with
traceback stays for causes generate's rule would hide.
Pydantic's frozen=True guards fields only; private attrs are stored
apart and settable. Pyrefly reads _public as a frozen field.
@huangjeff5
huangjeff5 force-pushed the fix/py-custom-agent-redact-errors branch from 9df9fdc to a6e6b21 Compare November 10, 2026 22:14
@huangjeff5 huangjeff5 changed the title fix(py/agents): redact agent exception text on the wire fix(py/agents)!: redact agent exception text on the wire Nov 11, 2026
@huangjeff5 huangjeff5 changed the title fix(py/agents)!: redact agent exception text on the wire fix(py/agents): redact agent exception text on the wire Nov 11, 2026

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comments

…edact-errors

# Conflicts:
#	packages/genkit/src/genkit/_ai/_generate.py
…ng Go

Go's wire.ClientMessage returns err.Error() when GENKIT_ENV=dev, for both a
served agent's failed turn (clientSafeError) and a served flow (clientError).
client_safe_failure and error_body now do the same. Status and HTTP code are
unchanged, so error_status still pairs with error_body.
@huangjeff5
huangjeff5 enabled auto-merge (squash) November 12, 2026 04:46
@huangjeff5
huangjeff5 merged commit 52ee00c into main Nov 12, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-custom-agent-redact-errors branch November 12, 2026 04:49
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