Skip to content

fix(py): plugins classify only the failures they can name - #6586

Merged
huangjeff5 merged 11 commits into
fix/py-action-raises-body-errorfrom
fix/py-plugins-classify-errors
Oct 7, 2026
Merged

huangjeff5 merged 11 commits into
fix/py-action-raises-body-errorfrom
fix/py-plugins-classify-errors

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Since #6576, ai.generate raises whatever the plugin raised: the plugin's status is the one Retry and Fallback act on. This PR fixes the first-party plugins that reported a status they didn't know (OK, UNKNOWN, INTERNAL for auth) or reported none where they did know one.

# 1. Gemini first, Claude as backup
response = await ai.generate(
    model='vertexai/gemini-flash-latest',
    prompt='Suggest a dish without peanuts.',
    use=[retry(ai), fallback(ai, models=['anthropic/claude-sonnet-4-5'])],
)

# --- BEFORE (on #6576) ---
# ADC missing on the host
# => GenkitError INTERNAL
#    Retry retries it, then Fallback calls Claude; the misconfiguration is hidden

# --- AFTER ---
# => GenkitError UNAUTHENTICATED
#    Retry and Fallback both stop; the caller sees the credential error

Main Changes

  • A plugin labels only what it knows: Middleware acts on a GenkitError whose status is in its list and treats anything else as unclassified (Retry retries, Fallback skips). Plugins now map a provider HTTP status, a provider error type, caller-input errors (INVALID_ARGUMENT), malformed replies (INTERNAL) and credential errors (UNAUTHENTICATED). Raw network errors (httpx, aiohttp, urllib3, an SDK's APIConnectionError, OllamaConnectionError) stay unclassified. A network blip is for Retry; Fallback should only switch models for a failure the plugin can name.
  • No more UNKNOWN or OK for a failure: UNKNOWN counts as classified but is in no default list, so it was worse than raw: Retry skipped it too. Anthropic connection errors, unlisted Bedrock botocore errors and AWS codes, and interactions-client connection errors were all UNKNOWN. An Anthropic overloaded_error that arrives mid-stream after a 200 came back as status='OK'. These now take the real status (HTTP status, or the in-band error type), or are re-raised raw when there isn't one.
  • A bad credential stops instead of falling back: Gemini's except Exception turned every failure into INTERNAL, including a missing ADC or a rejected token. Fallback then switched to the next model and hid the misconfiguration. A shared _auth.py maps DefaultCredentialsError / RefreshError to UNAUTHENTICATED across the generate, stream, embed, cache, Veo, evaluator and lazy-client paths. A refresh that google.auth marks retryable stays raw so Retry still covers token-endpoint blips. Everything else from that catch-all is now re-raised raw.
  • Caller mistakes and server mistakes are classified correctly: Ollama marked malformed server JSON INVALID_ARGUMENT, so Retry skipped a server fault. It marked an invalid num_predict INTERNAL, so Retry repeated a request that can never succeed. Both are swapped. A media URL that returns 403, 404 or 410 (Gemini, Ollama) is INVALID_ARGUMENT, not NOT_FOUND, so Fallback doesn't switch models over a broken image link.
  • Unmapped call paths now map provider errors: Gemini's embedder and context-cache calls, Ollama's embedder and list_actions, and OpenAI's in-band stream errors went through no mapping at all. They now go through wrap_http_error or the provider's error type. Bedrock's batch embedder now keeps retry_after_ms when it re-wraps a throttle error, so Retry waits as long as AWS asked.

Differs from Go

The rule matches Go's WrapAPIError helpers (compat_oai, anthropic, googlegenai): map the provider's reported status, leave transport errors raw, never emit UNKNOWN. Python goes further in three places:

  • Credential errors are UNAUTHENTICATED: Go leaves them raw, so Go's Retry reissues a bad credential until attempts run out. Python stops on the first try.
  • Broken media links are INVALID_ARGUMENT: Go has no media-fetch mapping.
  • Bedrock, Veo, evaluators, interactions client: no Go counterpart to compare.

Not changed

  • A down Ollama server stays unclassified: OllamaConnectionError is still a plain ConnectionError, same as Go's Ollama plugin and every other plugin's transport errors. Retry retries it; Fallback does not switch models. An app that wants "local first, cloud on outage" catches OllamaConnectionError itself.
  • No-status errors stay raw: an Ollama ResponseError(status_code=-1) mid-stream, or an Anthropic stream error of an unknown type, has no status to report. Retry handles it.
  • from_http_code still returns UNKNOWN for an unlisted 4xx (e.g. 418). That's core behavior and out of scope here.

Each fix has a test, and reverting any single fix fails at least one test. pyright, pyrefly and ty report the same counts as the base branch on the touched packages.

Since the action raises the body's exception, the plugin's status is
what Retry and Fallback act on. Plugins now map provider HTTP statuses,
provider error types, caller-input errors, malformed replies and
credential errors, and never report OK or UNKNOWN for a failure. Raw
network errors stay unclassified so Retry handles them and Fallback
skips them.

@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 standardizes and improves error classification and exception handling across multiple Genkit provider plugins, including Amazon Bedrock, Anthropic, Google GenAI, Ollama, OpenAI, and Vertex AI. The changes ensure that API, credential, and transport failures are mapped to standard GenkitError statuses (such as UNAUTHENTICATED, INVALID_ARGUMENT, RESOURCE_EXHAUSTED, FAILED_PRECONDITION, and INTERNAL) where appropriate, while allowing transient network or retryable errors to propagate raw so they can be retried. Additionally, mid-stream errors during streaming calls are now correctly classified, and extensive unit tests have been added to validate these error-mapping behaviors. I have no feedback to provide as there are no review comments.

…rors

# Conflicts:
#	py/packages/genkit-anthropic/tests/anthropic_error_handling_test.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_evaluators/_evaluation.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_google.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_models/_embedder.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_models/_gemini.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_models/_veo.py
#	py/packages/genkit-google-genai/test/google_plugin_test.py
#	py/packages/genkit-google-genai/test/models/googlegenai_embedder_test.py
#	py/packages/genkit-openai/tests/openai_model_test.py
@huangjeff5
huangjeff5 marked this pull request as ready for review October 7, 2026 04:34
@huangjeff5
huangjeff5 marked this pull request as draft October 7, 2026 04:34
@huangjeff5
huangjeff5 marked this pull request as ready for review October 7, 2026 05:02
@huangjeff5
huangjeff5 requested a review from apascal07 October 7, 2026 05:02
A down or slow Ollama server is a transport failure with no reported
status. Retry retries it; Fallback does not switch models on it.
…rors

# Conflicts:
#	py/packages/genkit-ollama/tests/plugin_api_test.py
#	py/packages/genkit-vertexai/tests/model_garden/model_garden_test.py
…rors

# Conflicts:
#	py/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/models.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_interactions/_client.py
…rors

# Conflicts:
#	py/packages/genkit-google-genai/src/genkit_google_genai/_models/_embedder.py
@huangjeff5
huangjeff5 merged commit 4fb72a7 into main Oct 7, 2026
22 of 25 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-plugins-classify-errors branch October 7, 2026 19:23
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.

2 participants