litellm/litellm-rust/crates/coroutine/AGENTS.md
yujonglee 8302f006dd
refactor(rust-bridge): return an unstarted Execution from native entrypoints and drive it in Python (#46299)
* refactor(rust-bridge): return an unstarted Execution from native entrypoints and drive it in Python

Rust's run_call built Python streams itself and imported
litellm.rust_bridge.streams by name, so sync calls were driven in Rust
while async calls came back as a Rust-made coroutine. Every native
inference entrypoint now returns one unstarted Execution and
public_execution.py drives it with lifecycle.run or lifecycle.drive,
wrapping streams and _hidden_params on the Python side. Rust only
imports the lifecycle step classes through StepTypes, and
python-bridge/src/lifecycle.rs, PythonLifecycle and streams.py are gone.

The adapter trusts the _native.pyi contract with a single cast instead
of a per-request runtime_checkable isinstance. Binding tests assert the
adapter starts the execution and returns its completed value rather
than checking callable identity.

* refactor(rust-bridge): fold lifecycle and public_execution into one execution module

The public stream wrappers only forwarded to the lifecycle streams and added the
header dict, so the streams now carry _hidden_params themselves. lifecycle.py no
longer held any call lifecycle, just the Execution driver, so it is renamed to
execution.py and absorbs the synchronous and asynchronous adapters

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(rust-bridge): keep execution.py free of third-party imports

The host-python tests load execution.py into a bare interpreter, so the
pydantic header validation broke them. The stream head comes from the
binding's encode_stream_head, which always builds a str-keyed dict

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(host-python): preserve pre-async hook coverage after execution handoff

* style(rust-bridge): sort litellm_host_python imports

Co-Authored-By: Yujong Lee <yujong@berri.ai>

* test(rust-bridge): drive unstarted native calls through execution in the split lifecycle tests

Co-Authored-By: Yujong Lee <yujong@berri.ai>

* fix(rust-bridge): mark execution TypeVar as Final

Co-Authored-By: Yujong Lee <yujong@berri.ai>

* test(rust-bridge): cover stream close, read failure, and sync suspension in execution

Co-Authored-By: Yujong Lee <yujong@berri.ai>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-14 17:50:44 -07:00

3.2 KiB

Requirements

Core must pause mid-call to ask the host for things it cannot do itself (Python callbacks, secret and token reads, before_send rewrites, stream demand), then continue where it stopped. Any change to this crate must keep every requirement below. The alternatives section says which one each rejected design breaks

  • R1 Core never calls the host: it names an op and waits for the answer, so it stays free of PyO3 and of any other host runtime
  • R2 Async host work is awaited by the host's own driver in the caller's asyncio task (litellm/rust_bridge/execution.py), so contextvars writes reach the caller. A Rust-side into_future would run it in a copied context
  • R3 The body awaits real I/O (HTTP, spawn_blocking, timers) between yields, so resume is itself a future driven by the caller's runtime
  • R4 Route code stays straight-line async (host.route(OcrOp::ReadDocument).await?) instead of hand-written states
  • R5 Each op fixes its answer type at compile time: a host cannot answer ReadDocument with a token, and core never matches a result variant it did not ask for
  • R6 A yield the body makes while being resumed is returned by that same poll, so the host driver's inline first poll needs no extra event-loop turn per op
  • R7 No task is spawned: cancel, or dropping the coroutine, drops the body, and nothing waits forever on an answer that cannot come
  • R8 Several yields can be pending at once, since route code hands clones of its Co to token providers and hooks
  • R9 Stable Rust

Other implementations and why they do not fit

  • Nightly std::ops::Coroutine: breaks R9, and its body cannot await futures between yields (R3)
  • genawaiter: resumes async bodies only with a noop waker, so the body cannot await real I/O (R3)
  • simple_coro: typestate Coro makes answering before resuming a compile-time rule, but its body cannot await arbitrary futures (R3) and its reply type R is fixed per coroutine (R5)
  • corosensei and other stackful coroutines: sync bodies on their own stack, no async I/O inside (R3)
  • A hand-written phase enum with an advance match (the old HostPhase): every await point becomes a state (R4)
  • An injected host trait with async fns: core would call the host itself (R1, R2)
  • Sans-IO, where core does no I/O and HTTP becomes one more host op: keeps every requirement and makes resume a pure step function, but HTTP, streaming, retries and timeouts would move out of core into every bridge. The one real alternative, not taken
  • Temporal's Rust workflow SDK (WorkflowFuture, WfContext) is the closest precedent: an async fn polled in place, commands sent over a channel with a oneshot to unblock them. Roles are inverted there (the language SDK owns the program, core answers), and its workflow body may not do real I/O

Tradeoffs accepted

  • A tokio mpsc channel plus a oneshot per yield instead of compiler-generated states
  • Protocol mistakes (resuming before answering, resuming after the end) are runtime ResumeErrors, not compile errors
  • Pending yields come out one per resume, in the order they were made, and each reply goes back to the yield that made it (R8)
  • An answer sent after its yield stopped waiting (for example the body timed out on it) is discarded, since the body already moved on