litellm/litellm-rust/crates/python-interop/AGENTS.md

2.1 KiB

  • Target invariants; implementation and runtime validation may lag these rules
  • Keep this crate a small, domain-neutral foundation: Python/Serde conversion and interpreter-boundary utilities
    • No LiteLLM domain dependencies, route types, callback policy, public API registration or cdylib build features
    • Generic code alone does not justify extraction: runtime integration stays in python-bridge/src/execution.rs, host adaptation in its lifecycle.rs
  • Use standard PyO3 ownership and conversion APIs
    • Prefer Bound<'py, T> for attached operations/results, Py<T> for retention; binding/unbinding does not copy payloads
    • Use pythonize for selected Serde data, never a JSON-text round trip; share conversion with Pythonized<T>
    • Preserve PythonizeError's standard conversion into PyErr; do not stringify original Python exceptions into new ValueErrors
    • Keep serializer-panic containment in Pythonized<T>: async output conversion can run in an unjoined blocking task and otherwise strand delivery
  • Use Python::detach for Rust-only work; Python operations require attachment
    • Keep diagnostic counters in the consumer; wrapper invocations do not measure every interpreter release
    • Release exclusive class borrows/locks before Python calls or decrements that can invoke finalizers; expose retained Python edges to GC without calling Python during traversal
  • Keep coroutine driving in the shared Python driver and native adapter
    • Driver: litellm/rust_bridge/lifecycle.py; handle: python-bridge/src/lifecycle.rs; native-backed behavior tests: python-bridge/tests/lifecycle.py
  • References: ownership, conversions, pythonize errors