Skip to main content
In this tutorial you take the feedback analysis pipeline from Tutorial 01 and make it production-ready: non-blocking async calls, token-by-token streaming to a UI, and concurrent batch processing. By the end you will have covered:
  • ainstruct() and the async session method naming convention
  • ModelOption.STREAM and mot.astream() for incremental output
  • wait_for_all_mots for fan-out concurrent generation
  • Context behaviour with concurrent async calls
Prerequisites: Tutorial 01 complete, pip install mellea, Ollama running locally with granite4.1:3b downloaded.

Step 1: Your first async call

Every sync method on MelleaSession has an a-prefixed async counterpart with the same signature and return type. Replace instruct() with ainstruct() and wrap the call in async def:
Sample output
Note: LLM output is non-deterministic. Your result will vary in wording, but should be a single sentence.
ainstruct() returns a ModelOutputThunk. await-ing it starts generation immediately; str(result) resolves the value when it is ready. Every other method follows the same pattern: achat(), aact(), aquery(), atransform(), avalidate().

Step 2: Streaming tokens

Enable streaming by passing ModelOption.STREAM: True in model_options. Consume chunks with mot.astream() as they arrive — useful for displaying output progressively rather than waiting for the full response:
Sample output
Note: With streaming enabled, tokens print incrementally as they arrive. The example above shows the completed output. Your terminal will display each token as it is generated. LLM output is non-deterministic. Your result will vary in wording, but should be a single sentence.
How astream() works:
  • Each call returns only the new content since the previous call.
  • When generation is complete, is_computed() returns True and the final astream() call returns the remaining content.
  • strategy=None is required to get a lazy thunk — without it, ainstruct() returns a pre-computed thunk and is_computed() is already True before the loop runs.
  • Do not call astream() from multiple coroutines on the same thunk simultaneously.

Step 3: Concurrent batch processing

The pipeline from Tutorial 01 processes one feedback item at a time, and each call blocks until the previous one completes. With ainstruct() you can fire all calls immediately and resolve them together. Use wait_for_all_mots to await a list of thunks concurrently:
Sample output
Note: LLM output is non-deterministic. You will see four summary lines, one per input, in the same order as FEEDBACK_BATCH.
The four requests are in flight simultaneously. Total wall-clock time is roughly the latency of the slowest single call, rather than the sum of all four.

Step 4: Mixing parallel and sequential steps

Some pipeline steps are independent; others depend on earlier results. You can resolve dependencies explicitly without blocking unrelated work. In the Tutorial 01 pipeline, extract_issues is independent of summarize — both take the raw feedback. Run them in parallel, then feed the resolved summary into classify_sentiment:
Sample output
Note: LLM output is non-deterministic, your output may vary based on model and temperature.

Step 5: Context and concurrency

By default start_session() uses SimpleContext, which is safe for concurrent async calls. If you switch to ChatContext, Mellea logs a warning because concurrent writes can corrupt the context state:
Note: This warning appears whenever ChatContext is used with async methods, even if you await each call sequentially. It is safe to ignore when you ensure each call is fully resolved before starting the next.
If you need ChatContext (for multi-turn conversation), await each call before starting the next:
Sample output
Note: LLM output is non-deterministic. Your result will vary in wording.
For parallel generation, keep the default SimpleContext.

What you built


See also: Async and Streaming (full API reference) | Tutorial 03: Using Generative Stubs