Skip to content

[py] client_options http_client breaks on a second event loop (OpenAI, Anthropic); Interactions models ignore max_retries #206

Open

Description

@huangjeff5

OpenAI(client_options={'http_client': httpx.AsyncClient(...)}) and the same on Anthropic break when Genkit runs on more than one event loop. The call on the second loop reaches the server, so it's billed, and then comes back as finish_reason='failed' with UNAVAILABLE.

I'm working on this. The planned fix is below.

Why

Both plugins build their SDK client with loop_local_client(lambda: AsyncOpenAI(**options)), so every event loop gets its own AsyncOpenAI / AsyncAnthropic. But every one of those clients is handed the same httpx.AsyncClient from client_options, and an httpx.AsyncClient ties its connection pool to the loop that first uses it.

Genkit runs on more than one loop in normal use: genkit start serves the Dev UI from its own thread and asyncio.run (_aio.py), and scripts can call asyncio.run more than once. So any app that passes its own http_client (for a proxy, mTLS or a custom transport) shares one pool between the app and the Dev UI.

Repro

A local HTTP/1.1 keep-alive server, one shared pooled client, and two asyncio.run calls, with max_retries=0 so we see each attempt:

# 1. One shared, pooled client passed through client_options
shared = httpx.AsyncClient(transport=httpx.AsyncHTTPTransport())
ai = Genkit(plugins=[OpenAI(api_key='k', base_url=f'{base}/v1', client_options={'http_client': shared, 'max_retries': 0})])

# 2. Call it from two event loops
for loop in (1, 2):
    response = asyncio.run(ai.generate(model='openai/gpt-4o', prompt='Suggest a dish.'))
    print(loop, repr(response.text), response.finish_reason, response.error and response.error.status)
# => 1 'Smoked Salmon Tartine' stop None
#    2 '' failed UNAVAILABLE
  • The server received both POSTs, so the failed call was billed.
  • Anthropic(client_options={'http_client': ...}) behaves the same way.
  • Without http_client in client_options, both loops succeed.
  • With the SDK default max_retries=2, the SDK re-sends the failed call, and that billed request fails on the same pool too.

Planned fix

Match what GoogleAI / VertexAI do after genkit-ai/genkit#6826: http_client takes a function that returns an httpx.AsyncClient, wrapped in loop_local_client so each loop builds its own client.

  • client_options={'http_client': lambda: httpx.AsyncClient(proxy=...)}. The plugin calls the function inside the per-loop SDK client factory.
  • Passing an httpx.AsyncClient instance raises a TypeError that shows the lambda form, the same message as GoogleAI.
  • Tests: the same two-asyncio.run test over a pooled transport against a localhost keep-alive server, for both plugins.

This is a breaking change for anyone passing an instance today. It goes in the migration notes alongside genkit-ai/genkit#6826.

Also in scope: Interactions models ignore max_retries

After genkit-ai/genkit#6826, GoogleAI(max_retries=N) maps to google-genai's HttpRetryOptions and covers every SDK-backed call: Gemini, Imagen, Veo including polling, Virtual Try-On, embedders and model listing. The Interactions models (Deep Research, Antigravity, Lyria) don't go through the SDK. They use Genkit's own httpx client in _interactions/_client.py, whose request() sends exactly one attempt, so max_retries doesn't reach them.

  • Today: a 429 or 503 on an Interactions create, get or cancel fails on the first attempt. genkit_middleware.Retry can re-run Antigravity and Lyria generate calls, since the client puts Retry-After into the error details, but nothing retries Deep Research operation checks or cancels.
  • Planned fix: pass the plugin's max_retries into ClientOptions and retry in request() with the same rules as google-genai: HTTP 408, 429, 500, 502, 503, 504 and transport errors, about 1, 2, 4 seconds doubling up to 60 with jitter, honoring Retry-After when present.
  • Billed creates: Deep Research and Antigravity creates are long-running and billed. A create retries only on 429 and 503, where the server rejected the request before starting work. Gets and cancels retry on everything transient.
  • Tests: a 503 then 200 succeeds with max_retries=1 on Antigravity and on a Deep Research check; a create that gets a 500 is not retried; no max_retries means one attempt.

Once this lands, the GoogleAI docstring and genkit-ai/genkit#6826's migration note drop the "Interactions models don't retry" caveat.

Activity

  1. self-assigned this
    on Oct 30, 2026
  2. added
    bug
    on Oct 30, 2026
  3. changed the title [-][py] OpenAI and Anthropic: client_options http_client breaks on a second event loop[/-] [+][py] client_options http_client breaks on a second event loop (OpenAI, Anthropic); Interactions models ignore max_retries[/+] on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions