invoke, ainvoke, stream, astream, batch, abatch- LCEL

Rahul S
2 min readSep 13, 2024

LCEL is a powerful feature of LangChain that allows for more flexible and composable chain construction.

These properties provide a comprehensive set of tools for working with LCEL chains in various scenarios.

The async versions (ainvoke, astream, abatch, astream_log) are particularly useful in applications that need to maintain responsiveness while processing, such as web servers or applications with user interfaces.

Here’s a brief summary of when we might use each:

  1. Use invoke and ainvoke for simple, one-off executions of your chain.
  2. Use stream and astream when you need to process or display results incrementally.
  3. Use batch and abatch when you have multiple inputs to process efficiently.
  4. Use astream_log when you need detailed visibility into the chain's execution process.

Let’s dive into each of these properties:

1. invoke

  • Purpose: Synchronously executes the chain or component.
  • Usage: Used when you want to run the chain and wait for the result before proceeding.
  • Example:
result = my_chain.invoke({"input": "Hello, world!"}) 
print(result)

2. ainvoke

  • Purpose: Asynchronously executes the chain or component.
  • Usage: Used when you…

--

--