Repository navigation
[Feature] create_and_wait helpers for Flows generations and template runs - #889
Conversation
client.flows.{image,video,text_to_speech}.create_and_wait and
client.flows.templates.runs.create_and_wait (sync and async) start the
work, then poll the GET endpoint, waiting the server's Retry-After (with
a minimum interval and jitter) between checks, until the status is
completed or failed. An optional timeout raises FlowsWaitTimeoutError
carrying the id so callers can resume with get.
FLO-350
Co-Authored-By: Claude Opus 5.5 <[email protected]>
PR SummaryLow Risk Overview New Tests cover pacing, timeouts, async paths, validation, and signature/doc parity with generated Reviewed by Cursor Bugbot for commit 7f7baee. Bugbot is set up for automated code reviews on this repo. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dd0db64. Configure here.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Narrow generation results to TerminalMediaGenerationResponse - Reject min_poll_interval below 0.5s to rule out tight poll loops - Keep additional_body_parameters off the status GETs - Stop naming the Retry-After header in public docstrings - Point run params at `create` and test signature parity with it Co-Authored-By: Claude Opus 5.5 <[email protected]>
FLO-349's shortest Retry-After is 3s (image, audio, text), and the web app and MCP view poll at 3s or slower. A 1s default would poll three times faster than the server intends whenever the header is missing, which is every response until FLO-349 ships. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The wrappers now copy the parameter docs (and, for generations, the summary) from the generated `create` docstring at import time, so the fuller descriptions of `inputs`, `version_id` and `webhook`, plus the TTS billing note, carry over and track regenerations. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Docstrings assigned at runtime don't show in IDE hovers, and the other hand-written wrappers (music, speech-to-text) use plain docstrings. The parameter docs are copied from the generated `create`, and the parity test now fails if any copied entry no longer matches it verbatim. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_parse_retry_after` and `_add_positive_jitter` are private to the generated core and can change on regeneration. Keep a small Retry-After parser (seconds or HTTP date) and the 0-20% jitter in flows_custom. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in_seconds `timeout` clashed with the client's per-request `timeout`. The new names follow `request_options["timeout_in_seconds"]` and match the JS SDK's `waitTimeoutInSeconds` / `minPollIntervalInSeconds`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Non-finite polling intervals bypass validation and can cause sleep failures or indefinite waits.
1 open finding
What changed in this PR
Adds synchronous and asynchronous create_and_wait helpers for Flows media generations and template runs.
Changes:
- Implements polling,
Retry-Afterhandling, jitter, and timeout errors. - Wires custom Flows clients into the SDK.
- Adds comprehensive polling and parity tests.
| File | Description |
|---|---|
.fernignore |
Protects the custom Flows wrapper from regeneration. |
src/elevenlabs/client.py |
Exposes custom sync and async Flows clients. |
src/elevenlabs/flows_custom.py |
Implements polling helpers and client wrappers. |
tests/test_flows_create_and_wait.py |
Tests polling, timeout, request, and API parity behavior. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
NaN slips past the min_poll_interval floor check and an infinite interval never polls; a NaN wait_timeout_in_seconds silently disables the timeout. All now raise ValueError before the create request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
wait_timeout_in_seconds must now be finite and >= 0, like waitTimeoutInSeconds in elevenlabs-js. The template-run timeout message names the template and says runs.get needs both template_id and id. Also test that an HTTP-date Retry-After sets the poll delay, and a past date falls back to the minimum. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Matches elevenlabs-js 3e8fb956: the timeout only has to be at least 0, so inf waits indefinitely like None, while NaN and negatives still raise. Co-Authored-By: Claude Opus 5.5 <[email protected]>


FLO-350: quickstarts should get a Flows result in one call, without a hand-written poll loop or webhook plumbing (like OpenAI's
create_and_poll)._should_retryonly covers 408/409/429/5xx, so an in-progress 200 returns straight away. JS counterpart: elevenlabs/elevenlabs-js#469.Warning
Merge after xi#52056 is live, together with elevenlabs-js#469, then cut a release. Without xi#52056's
Retry-After, the helper polls at its 3s minimum. xi#52081 and elevenlabs-dx#2806 can't merge until this is in a published release.Usage
Design
src/elevenlabs/flows_custom.py, added to.fernignore. Subclasses of the generated flows, templates, runs, image, video and TTS clients, sync and async.client.pysetsself._flowsto the wrapper, the same pattern as music/STT/speech engine.with_raw_response, so the create response'sRetry-After(FLO-349 sets it on creates too) sets the first wait.Retry-Afteror min, min) × (1 + 0–20%). A missing, unparseable or0header falls back tomin_poll_interval_in_seconds(default 3s, the server's shortestRetry-Afterin FLO-349).Retry-Afteris read as whole seconds or an HTTP date. Transport retries still handle 429s.ValueErrorbefore the create:min_poll_interval_in_secondsmust be finite and at least 0.5;wait_timeout_in_seconds, if set, must be at least 0 (NaN is rejected,float("inf")means no timeout, and 0 times out right after the create).request_optionsapplies to every request exceptadditional_body_parameters, which only the create gets.completed/failedare terminal. Generation helpers returnTerminalMediaGenerationResponse(theCompleted | Failedunion); runs returnTemplateRunResponse, which the API doesn't discriminate on status. Afailedresult is returned, not raised.wait_timeout_in_seconds, the last sleep is cut short at the deadline and one final GET runs. If the work still isn't finished, it raisesFlowsWaitTimeoutError(id,template_id,wait_timeout_in_seconds,last_response). With no timeout set (orfloat("inf")), it waits indefinitely, like OpenAI. A template-run timeout message names the template and saysruns.getneeds bothtemplate_idandid. The names followrequest_options["timeout_in_seconds"]and match JS (waitTimeoutInSeconds,minPollIntervalInSeconds); plaintimeoutwould clash with the client's per-requesttimeout.Notes
FlowsWaitTimeoutErroris a plainException, notApiError, because no HTTP status applies. Import it fromelevenlabs.flows_custom, since the package__init__is generated.*_custom.py+.fernignore, subclass wiring inclient.py,Autogenerated*aliases,OMIT, static docstrings duplicated for sync/async (as inmusic_custom.py),ValueErrorfor bad input.FlowsWaitTimeoutErroris the first hand-written exception (webhooks_custom.pyreuses the generatedBadRequestError, but a wait timeout has no HTTP status).Retry-Afterparsing and jitter live inflows_custom.pyinstead of importingcore.http_client's private_parse_retry_after/_add_positive_jitter, which can change on regeneration. (Fern'sretry-after-msbranch compares astrto0and silently never matches, so dropping it loses nothing.)MockTransportinstead ofpatch.object(Autogenerated..., "create"), because the helper reads response headers.create_and_waitagainst the generatedcreate: same parameters, and each parameter's docs copied word for word, so a regeneration can't silently leave the wrapper behind.TerminalMediaGenerationResponseis the only type alias a wrapper exports.ruff formaton the test file: ruff rewrites it to parenthesized context managers, which aren't valid on Python 3.8 (python = "^3.8").flows_template_runappears in the API definition.Testing
tests/test_flows_create_and_wait.py: covers Retry-After pacing and jitter, the min fallback, failed generations returned rather than raised, timeout errors carrying the id (andtemplate_idfor runs, named in the message), the no-poll path when create returns a terminal status, the async image and run paths, themin_poll_interval_in_secondsfloor and non-finite/negative wait options, an infinite timeout polling through,additional_body_parametersstaying off the GETs, and signature and docstring parity with each generatedcreate, andRetry-AfterHTTP dates (future sets the delay, past falls back to the min). Uses httpxMockTransportwith sleeps patched.pytest tests/test_flows_create_and_wait.py: 26/26 pass.mypyon the new and changed files is clean.🤖 Generated with Claude Code