MCPcopy Create free account
hub / github.com/NVIDIA/NeMo-Relay / wrap_py_collector_fn

Function wrap_py_collector_fn

crates/python/src/py_callable.rs:782–795  ·  view source on GitHub ↗

Wrap a Python callable `(Any) -> None` as a collector for streaming LLM calls. The collector is invoked with each intercepted chunk (after stream response intercepts have been applied). It receives a single JSON-converted Python object argument. If the Python callable raises an exception, it is converted to a `FlowError::Internal` and returned as `Err`, which terminates the stream. If the callabl

(
    py_fn: Py<PyAny>,
)

Source from the content-addressed store, hash-verified

780/// stream. If the callable returns normally (including `None`), the collector
781/// returns `Ok(())`.
782pub fn wrap_py_collector_fn(
783 py_fn: Py<PyAny>,
784) -> Box<dyn FnMut(Json) -> std::result::Result<(), FlowError> + Send> {
785 Box::new(move |chunk: Json| {
786 Python::attach(|py| {
787 let py_chunk = json_to_py(py, &chunk)
788 .map_err(|e| FlowError::Internal(format!("collector json_to_py failed: {e}")))?;
789 py_fn
790 .call1(py, (py_chunk,))
791 .map_err(|e| FlowError::Internal(format!("Python collector error: {e}")))?;
792 Ok(())
793 })
794 })
795}
796
797/// Wrap a Python callable `() -> Any` as a finalizer for streaming LLM calls.
798///

Calls 1

json_to_pyFunction · 0.85