Common Errors — Genkit Python 0.13
Find the symptom, apply the fix. Every message below comes from a real 0.13 run. Upgrading a 0.12 codebase? Read Migrating from 0.12 to 0.13.
- ai.generate Returns Empty Text and Does Not Raise: Lines 25-57
- Thinking level MINIMAL is not supported for this model: Lines 61-80
- ImportError on ActionRunContext, ToolRunContext, MediaPart, TextPart, or genkit.agent: Lines 84-119
- GenkitError INVALID_ARGUMENT on Config Keys: Lines 123-151
- GenkitError INVALID_ARGUMENT: API Key in config: Lines 155-167
- GenkitError NOT_FOUND: Unprefixed Model String: Lines 171-183
- Vertex AI Fails on gemini-flash-latest: Lines 187-197
- TypeError When Defining or Calling Flows and Tools: Lines 201-232
- Served Flow Returns 404, 500, or the Wrong Auth Status: Lines 236-271
- response.output Is None on Structured Output: Lines 275-290
- response.json Is Not the Model Output: Lines 294-305
- Prompt Variable Sent Literally: Lines 309-319
- Streaming TypeError or a Flow That Keeps Running After break: Lines 323-344
- Cloud Telemetry Enabled but Spans Do Not Reach Cloud Trace: Lines 348-359
- Interrupts and Agent Session Pitfalls: Lines 363-392
ai.generate Returns Empty Text and Does Not Raise
ai.generate boxes model-side failures into the response: provider rejections,
UNAVAILABLE, rate limits, and model IDs the provider does not serve. You get
response.text == '', response.finish_reason == 'failed', and a Failure on
response.error. Code that only reads response.text silently continues with
an empty string.
from genkit import Genkit
from genkit_google_genai import GoogleAI
ai = Genkit(plugins=[GoogleAI()])
# 1. Prefixed but unserved model ID: no exception
res = await ai.generate(model='googleai/gemini-9-ultra', prompt='Suggest a dish.')
print(repr(res.text), res.finish_reason)
# => '' FinishReason.FAILED
# 2. Check response.error after every call
if res.error:
print(res.error.status, res.error.message)
# => NOT_FOUND models/gemini-9-ultra is not found for API version v1beta, or is not supported for generateContent. ...Problems caught before the request is sent raise instead:
ValueErrorfromGoogleAI()whenGEMINI_API_KEYis unset ([Genkit] GEMINI_API_KEY environment variable not set.).GenkitErrorUNAUTHENTICATEDon the firstai.generatewhen the key is invalid (API key not valid. Please pass a valid API key.).GenkitErrorfor an unprefixed model string, unknown config keys, andapi_keyin config (sections below).
Thinking level MINIMAL is not supported for this model
gemini-flash-latest rejects thinking_level='MINIMAL'. The call returns a
failed response (see above), not an exception:
from genkit_google_genai import GeminiConfig
res = await ai.generate(prompt='Name one dessert.', config=GeminiConfig(thinking_level='MINIMAL'))
print(res.error.status, res.error.message)
# => INVALID_ARGUMENT Thinking level MINIMAL is not supported for this model. Please retry with other thinking level.Use 'LOW', 'MEDIUM', or 'HIGH':
res = await ai.generate(prompt='Name one dessert.', config=GeminiConfig(thinking_level='LOW'))
print(res.text)
# => Tiramisu.ImportError on ActionRunContext, ToolRunContext, MediaPart, TextPart, or genkit.agent
ImportError: cannot import name 'ActionRunContext' from 'genkit'
ImportError: cannot import name 'MediaPart' from 'genkit'
ModuleNotFoundError: No module named 'genkit.agent'
AttributeError: 'Genkit' object has no attribute 'define_agent'
ValidationError: 1 validation error for Part
Value error, Part(root=...) is gone; use Part.from_text or Part(text=...)Each symbol has one public home:
# Contexts
from genkit import FlowContext, ToolContext
from genkit.model import ModelContext
# Parts
from genkit import Media, Message, Part, Role
text_part = Part.from_text('Hello')
img_part = Part.from_media('https://example.com/photo.jpg', content_type='image/jpeg')
# Experimental agents and integrations (define_agent exists only on genkit.exp.Genkit)
from genkit.exp import Genkit, current_session
from genkit.exp.agent import Agent, InMemorySessionStore, Session
from genkit_fastapi.exp import serve_agent
from genkit_middleware.exp import ArtifactsTwo different Middleware imports:
from genkit_middleware import Middleware, Retry, ToolApprovalimports theMiddleware()plugin and built-in middleware.from genkit.middleware import Middleware, MiddlewareContextimports the base class for custom middleware.
GenkitError INVALID_ARGUMENT on Config Keys
Genkit validates config before calling the model. Typos, nested dicts, and fields that moved off config all raise:
INVALID_ARGUMENT: googleai/gemini-flash-latest: unknown config key 'temprature'; put provider-only settings in config['extra']
INVALID_ARGUMENT: thinking_config was flattened; use include_thoughts / thinking_budget / thinking_level
INVALID_ARGUMENT: googleai/gemini-flash-latest: unknown config key 'tool_choice'; put provider-only settings in config['extra']Use flat typed config, put provider-native keys in extra, and pass
tool_choice on the call:
from genkit_google_genai import GeminiConfig
cfg = GeminiConfig(
temperature=0.2,
thinking_level='HIGH',
include_thoughts=True,
extra={'seed': 7},
)
res = await ai.generate(prompt='Pick a wine for salmon.', config=cfg, tool_choice='required', tools=[...])GeminiConfig.extra is wire-shaped and deep-merged into the request body, so
nested provider fields go under their wire key (for example
extra={'generationConfig': {...}}).
GenkitError INVALID_ARGUMENT: API Key in config
INVALID_ARGUMENT: API key belongs in context.secrets, not config. Pass the key as context={'secrets': {'api_key': ...}}.Set the key on the plugin, or pass a per-request key in context:
# Whole app: GoogleAI(api_key=...), OpenAI(api_key=...)
# Per request:
await ai.generate(prompt='Hello', context={'secrets': {'api_key': user_key}})GenkitError NOT_FOUND: Unprefixed Model String
NOT_FOUND: Failed to resolve model 'gemini-flash-latest'. Ensure the model name includes the plugin namespace (e.g., 'plugin/model').Plain strings need the <plugin>/ prefix ('googleai/gemini-flash-latest',
'openai/gpt-4o'). Model ref helpers (GoogleAI.gemini('gemini-flash-latest'),
Anthropic.claude('claude-sonnet-4-6'), OpenAI.gpt('gpt-5.2')) add it for
you.
A prefixed ID the provider does not serve resolves and fails at call time on
response.error instead (see the first section).
Vertex AI Fails on gemini-flash-latest
Vertex AI does not serve the Gemini API's rotating -latest aliases
(gemini-flash-latest, gemini-flash-lite-latest, gemini-pro-latest). The
Vertex plugin's known-model list omits them. Use a versioned ID:
from genkit_google_genai import VertexAI
ai = Genkit(plugins=[VertexAI(location='us-central1')], model=VertexAI.gemini('gemini-3.5-flash'))TypeError When Defining or Calling Flows and Tools
TypeError: Tool function must be async. Got sync function: lookup_reservation
TypeError: Flow must be async: sync_menu
TypeError: tool 'check_allergens' takes one input, but 'allergen' is a second parameter. Put the fields on one input model, or annotate 'allergen' as ToolContext.
TypeError: Genkit() takes no positional arguments, got 1. Pass keyword arguments instead, e.g. Genkit(plugins=[...], model="...").
TypeError: Action.run() takes from 1 to 2 positional arguments but 3 were given@ai.tool()and@ai.flow()functions must beasync def.- Each takes at most one input parameter. A context parameter must be annotated
(
ctx: FlowContextorctx: ToolContext); an unannotatedctxcounts as a second input. Wrap multi-field input in one PydanticBaseModel. - Constructors (
Genkit, plugins,GenkitError) and every option after the input on.run(...)are keyword-only:
from pydantic import BaseModel
from genkit import ToolContext
class AllergenCheck(BaseModel):
dish: str
allergen: str
@ai.tool()
async def check_allergens(check: AllergenCheck, ctx: ToolContext) -> bool:
"""Return True if the dish contains the allergen."""
return check.allergen in MENU[check.dish].allergens
res = await plan_menu.run(payload, on_chunk=handle_chunk, context={'uid': 'u1'})Served Flow Returns 404, 500, or the Wrong Auth Status
Over HTTP (serve_flow, serve_agent, genkit_fastapi_handler,
handle_genkit_request):
404 {"detail":"Not Found"}: the route is the Python function name.menu_suggestion_flowmounts atPOST /menu_suggestion_flow, not/menuSuggestionFlow. Override withserve_flow(flow, base_path=...).500 {"message":"Internal Error","status":"INTERNAL"}: the flow raisedGenkitErroror any non-PublicErrorexception. Genkit logs the traceback server-side and redacts the body. RaisePublicErrorfor errors the client should see.400 ... "status":"INVALID_ARGUMENT": the requestdatafailed input validation.500 Internal Server Error(plain text) from acontext_dependency:serve_flow(..., context_dependency=...)runs a FastAPI dependency before Genkit's handler, so aPublicErrorraised there is not mapped. Raisefastapi.HTTPException(status_code=401)in dependencies;PublicError('UNAUTHENTICATED', ...)maps to 401 only inside agenkit_fastapi_handlercontext_provideror the flow itself.
from genkit import PublicError
@ai.flow()
async def get_invoice(invoice_id: str) -> dict:
inv = await find_invoice(invoice_id)
if inv is None:
raise PublicError('NOT_FOUND', f'Invoice {invoice_id} not found')
return inv
# POST /get_invoice {"data": "INV-9"}
# => 404 {"message":"Invoice INV-9 not found","status":"NOT_FOUND"}Full status mapping: FastAPI.
response.output Is None on Structured Output
When a JSON response is truncated (often by max_output_tokens) or does not
parse against the schema, response.output is None and
response.error.reason is ErrorReason.INVALID_OUTPUT:
from genkit import ErrorReason
res = await ai.generate(prompt='Summarize this invoice.', output_schema=InvoiceSummary)
if res.output is None and res.error and res.error.reason == ErrorReason.INVALID_OUTPUT:
print(res.error.message)
# => Model output was not valid JSON for the requested schema: {"vendor": "Acme", "total": 12Raise max_output_tokens or simplify the schema, then retry.
response.json Is Not the Model Output
response.json is Pydantic's deprecated BaseModel.json method. Calling it
serializes the whole ModelResponse and emits PydanticDeprecatedSince20.
Read:
response.textfor plain text.response.outputfor structured output.response.messagefor the full message.
GenkitError exposes e.message, e.status, e.details, e.reason, and
e.__cause__. e.original_message raises AttributeError.
Prompt Variable Sent Literally
ai.generate(prompt=..., system=...) sends strings verbatim. The model receives
Say hello to {{name}}. as written. Render templates through ai.define_prompt
or a .prompt file:
greet = ai.define_prompt(name='greet', prompt='Say hello to {{name}}.')
res = await greet({'name': 'Sam'})
# Model receives: Say hello to Sam.Streaming TypeError or a Flow That Keeps Running After break
TypeError: object ModelStreamResponse can't be used in 'await' expression- Do not
awaitai.generate_stream(...),flow.stream(...), orchat.send_stream(...). Iterate the stream, thenawaitthe final response:sr = ai.generate_stream(prompt='Describe the tasting menu.') async for chunk in sr.stream: print(chunk.text, end='') final = await sr.response - To stop a flow stream early, use
async with. Leaving the block cancels the flow:async with course_flow.stream(payload) as stream: async for chunk in stream: if chunk == 'dessert': break # course_flow receives CancelledError
Cloud Telemetry Enabled but Spans Do Not Reach Cloud Trace
By default in local development (GENKIT_ENV=dev),
enable_google_cloud_telemetry() runs in local-only mode to prevent polluting
Cloud Trace with local run data. To export traces to GCP from local development,
pass force_dev_export=True:
from genkit_google_cloud import enable_google_cloud_telemetry
enable_google_cloud_telemetry(project='bistro-prod', force_dev_export=True)Interrupts and Agent Session Pitfalls
except Exception:does not catchInterrupt:InterruptsubclassesBaseException, so a tool's catch-all cannot swallow approval interrupts. Put side-effect cleanup infinally.- Approval resume: build restart parts with
part.restart(resumed_metadata={'tool_approved': True})on items fromres.interrupts(see agents-human-in-the-loop.md). ImportError: cannot import name 'AgentError': failed turns do not raise.chat.send()returns a turn withfinish_reason == AgentFinishReason.FAILEDandres.error.AgentInitErrorraises fromagent.chat(...)on bad init;sendraisesGenkitErroronly when no turn result arrives.res.snapshot_idisNone: the agent has no store. Passstore=InMemorySessionStore()todefine_agent(see agents.md).AgentInitError: FAILED_PRECONDITION: Cannot send 'state' to a server-managed agent (one with a store). Send 'snapshot_id' or 'session_id' instead.: store-backed chats resume by ID. Mutate state inside the turn (see agents-state.md).- Resume picks the wrong branch after a fork:
session_idresumes the most recently created leaf. Pass the branch'ssnapshot_id(see agents-branching.md). - Resume after a failed turn: only
completedsnapshots resume bysnapshot_id. Useagent.chat(session_id=...)to continue from the last good turn. chat.abort()does nothing: during a foregroundsendit is a no-op. Run the turn withdetach()and callawait task.abort()(see agents-background.md).- Remote client mismatches: align
state_managementwith the server and omit trailing slashes on the base URL (see agents-http.md).