We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience and analyze website traffic…

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
GLM-5.1 API Benchmarks: Latency, Throughput & CostGLM-5.1 API Benchmarks: Latency, Throughput & Cost<p>Z.ai&#8217;s GLM-5.1 is an April 2026 open-weight reasoning model built for long-horizon agentic engineering — and accessing it effectively means navigating a real spread of provider options. Across 10 benchmarked API providers, blended pricing ranges from $0.74 to $1.70 per 1M tokens, output speed from 33.8 to 175.2 t/s, and the fastest provider is 5.2x [&hellip;]</p>
OpenClaw Use Cases That Deliver Real ROIOpenClaw Use Cases That Deliver Real ROI<p>An OpenClaw agent that reads your email, opens pull requests, and watches a server is only useful if running it doesn&#8217;t feel like leaving the meter running. That&#8217;s the quiet constraint behind every OpenClaw use cases discussion. Most of the workflows people show off (morning briefings, multi-agent research, ambient monitoring) only make sense if each [&hellip;]</p>
Frontier-Level Agents on Open Models: LangChain Deep Agents + NVIDIA Nemotron 3 Ultra, Live on DeepInfraFrontier-Level Agents on Open Models: LangChain Deep Agents + NVIDIA Nemotron 3 Ultra, Live on DeepInfraOpen models have reached frontier-level agent performance. Starting today, you can point LangChain Deep Agents at NVIDIA Nemotron 3 Ultra running on DeepInfra and get top-tier agent accuracy at roughly 10x lower cost than leading closed models.