DeepInfra raises $107M Series B to scale the inference cloud — read the announcement

Langchain improvements: async and streaming

Published on 2023.10.25 by Iskren Chernev
Langchain improvements: async and streaming

Starting from langchain v0.0.322 you can make efficient async generation and streaming tokens with deepinfra.

Async generation

The deepinfra wrapper now supports native async calls, so you can expect more performance (no more threads per invocation) from your async pipelines.

from langchain.llms.deepinfra import DeepInfra

async def async_predict():
    llm = DeepInfra(model_id="meta-llama/Llama-2-7b-chat-hf")
    output = await llm.apredict("What is 2 + 2?")
    print(output)
copy

Response streaming

Streaming lets you receive each token of the response as it gets generated. This is indispensable in user-facing applications.

def streaming():
    llm = DeepInfra(model_id="meta-llama/Llama-2-7b-chat-hf")
    for chunk in llm.stream("[INST] Hello [/INST] "):
        print(chunk, end='', flush=True)
    print()
copy

You can also use the asynchronous streaming API, natively implemented underneath.

async def async_streaming():
    llm = DeepInfra(model_id="meta-llama/Llama-2-7b-chat-hf")
    async for chunk in llm.astream("[INST] Hello [/INST] "):
        print(chunk, end='', flush=True)
    print()
copy
Related articles
Build a Streaming Chat Backend in 10 MinutesBuild a Streaming Chat Backend in 10 Minutes<p>When large language models move from demos into real systems, expectations change. The goal is no longer to produce clever text, but to deliver predictable latency, responsive behavior, and reliable infrastructure characteristics. In chat-based systems, especially, how fast a response starts often matters more than how fast it finishes. This is where token streaming becomes [&hellip;]</p>
Best API Providers for Wan 3.0 IntegrationBest API Providers for Wan 3.0 Integration<p>Wan3.0 is Alibaba&#8217;s all-in-one video generation model. It accepts text, images, video, audio, documents, and web pages, and it produces up to 30 seconds of 1080P video with synchronized audio. Production use raises practical questions. Video jobs run asynchronously, reference inputs can be large, and each provider exposes a different slice of the model&#8217;s features [&hellip;]</p>
Gemini 3.7 Flash: What's New, Benchmarks & API GuideGemini 3.7 Flash: What's New, Benchmarks & API Guide<p>Gemini 3.7 Flash is Google&#8217;s Flash-tier reasoning model. It takes text, images, video, audio and PDF files as input, returns text, and has a 1,000,000-token context window on DeepInfra. Google DeepMind released it on August 13, 2026, about three weeks after Gemini 3.6 Flash, and positioned it as its workhorse model for coding and agents. [&hellip;]</p>