Skip to main content

RunnableParallel

Struct RunnableParallel 

Source
pub struct RunnableParallel<I: Send + Sync + 'static> { /* private fields */ }
Expand description

A Runnable that runs multiple steps in parallel on the same input.

Each step is identified by a string key. The output is a HashMap<String, Value> where each key maps to the corresponding step’s output (serialized as serde_json::Value).

§Example

let parallel = RunnableParallel::<String>::new()
    .with("length", RunnableLambda::new_sync(|s: String| s.len() as i64))
    .with("upper", RunnableLambda::new_sync(|s: String| s.to_uppercase()));

let result = parallel.invoke("hello".to_string(), None).await?;
// result = {"length": 5, "upper": "HELLO"}

Implementations§

Source§

impl<I: Clone + Send + Sync + 'static> RunnableParallel<I>

Source

pub fn new() -> Self

Create an empty parallel runnable.

Source

pub fn with<O, R>(self, key: &str, runnable: R) -> Self
where O: Serialize + Send + Sync + 'static, R: Runnable<I, O> + Send + Sync + 'static, R::Error: Into<LcelError>,

Add a step with the given key.

The step’s output will be serialized to serde_json::Value and stored under the key in the output HashMap.

Source

pub fn len(&self) -> usize

Number of parallel steps.

Source

pub fn is_empty(&self) -> bool

Whether there are no steps.

Source

pub fn assign<O, R>( self, key: &str, runnable: R, ) -> RunnableSequence<I, HashMap<String, Value>>
where I: 'static, O: Serialize + Send + Sync + 'static, R: Runnable<HashMap<String, Value>, O> + Send + Sync + 'static, R::Error: Into<LcelError>,

Add an assign step that injects a new key into the output HashMap.

This is the LCEL equivalent of Python’s RunnableParallel.assign(). It pipes the parallel output (a HashMap<String, Value>) through a RunnableAssign that runs the given runnable on the HashMap and merges the result under the specified key.

§Example
let chain = RunnableParallel::<String>::new()
    .with("context", retriever.pipe(format_docs))
    .assign("question", RunnableLambda::new_sync(|m: HashMap<String, Value>| {
        m.get("context").map(|c| c.to_string()).unwrap_or_default()
    }))
    .pipe(prompt_template)
    .pipe(llm);
§How it works

assign() returns self.pipe(RunnableAssign). The RunnableAssign receives the HashMap output from the parallel step, runs the provided runnable on it, and merges the result back.

Trait Implementations§

Source§

impl<I: Send + Sync + 'static> Debug for RunnableParallel<I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I: Clone + Send + Sync + 'static> Default for RunnableParallel<I>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<I: Clone + Send + Sync + 'static> Runnable<I, HashMap<String, Value>> for RunnableParallel<I>

Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input: I, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, Value>, LcelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute all steps in parallel using tokio tasks.

Source§

fn batch<'life0, 'async_trait>( &'life0 self, inputs: Vec<I>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<HashMap<String, Value>>, LcelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Batch: each step processes all inputs independently.

Source§

fn stream<'life0, 'async_trait>( &'life0 self, input: I, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<HashMap<String, Value>, LcelError>> + Send>>, LcelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream: invoke and return single-element stream.

Source§

type Error = LcelError

Error type.
Source§

fn batch_as_completed<'life0, 'async_trait>( &'life0 self, inputs: Vec<Input>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Batch processing that returns results in completion order. Read more
Source§

fn transform<'life0, 'async_trait>( &'life0 self, input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream-to-stream transformation - the core of LCEL streaming. Read more

Auto Trait Implementations§

§

impl<I> !RefUnwindSafe for RunnableParallel<I>

§

impl<I> !UnwindSafe for RunnableParallel<I>

§

impl<I> Freeze for RunnableParallel<I>
where Vec<(String, Arc<dyn ParallelStep<I>>)>: Freeze,

§

impl<I> Send for RunnableParallel<I>
where Vec<(String, Arc<dyn ParallelStep<I>>)>: Send,

§

impl<I> Sync for RunnableParallel<I>
where Vec<(String, Arc<dyn ParallelStep<I>>)>: Sync,

§

impl<I> Unpin for RunnableParallel<I>
where Vec<(String, Arc<dyn ParallelStep<I>>)>: Unpin,

§

impl<I> UnsafeUnpin for RunnableParallel<I>
where Vec<(String, Arc<dyn ParallelStep<I>>)>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<I, O, R> RunnableExt<I, O> for R
where I: Send + Sync + 'static, O: Send + Sync + 'static, R: Runnable<I, O> + 'static, <R as Runnable<I, O>>::Error: Into<LcelError>,

Source§

fn pipe<O2, R2>(self, other: R2) -> RunnableSequence<Input, O2>
where O2: Send + Sync + 'static, R2: Runnable<Output, O2> + Send + Sync + 'static, R2::Error: Into<LcelError>,

Pipe the output of this runnable into another runnable. Read more
Source§

fn into_sequence(self) -> RunnableSequence<Input, Output>

Create a RunnableSequence from this runnable as a single step. Read more
Source§

fn with_fallbacks<R>( self, fallbacks: Vec<R>, ) -> RunnableWithFallbacks<Input, Output>
where Input: Clone, R: Runnable<Input, Output> + Send + Sync + 'static, R::Error: Into<LcelError>,

Add fallback runnables that are tried if this one fails. Read more
Source§

fn with_retry(self, retry_config: RetryConfig) -> RunnableRetry<Input, Output>
where Input: Clone,

Wrap this runnable with retry logic using exponential backoff. Read more
Source§

fn configurable_alternatives<K, R>( self, which: impl Into<String>, default_key: impl Into<String>, alternatives: Vec<(K, R)>, ) -> RunnableConfigurable<Input, Output>
where K: Into<String>, R: Runnable<Input, Output> + Send + Sync + 'static, R::Error: Into<LcelError>,

Route between a default runnable and named alternatives at invoke time. Read more
Source§

fn configurable_fields(self) -> RunnableConfigurableFields<Input, Output>

Override recognized config fields at invoke time from config.configurable (Python’s Runnable.configurable_fields). Read more
Source§

impl<I, R> RunnablePick<I> for R
where I: Send + Sync + 'static, R: Runnable<I, HashMap<String, Value>> + 'static, <R as Runnable<I, HashMap<String, Value>>>::Error: Into<LcelError>,

Source§

fn pick<K>( self, keys: impl IntoIterator<Item = K>, ) -> RunnableSequence<I, HashMap<String, Value>>
where K: Into<String>,

Keep only the given keys of the dict output, dropping everything else. Read more
Source§

fn pluck(self, key: impl Into<String>) -> RunnableSequence<I, Value>

Pull a single value out of the dict output. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more