AIToday
AI Coding AssistantsOpen-Source AIZenn AI/MLPublished: Oct 25, 2026, 13:01 JST

Ollama streaming chat built with just urllib, no pip installs

Send on LINE
Ollama streaming chat built with just urllib, no pip installs

3 Key Points

  1. What happened

    A writer ran a chat_stream.py script against Ollama 0.40.4 on Windows 11 and Python 3.11, using only urllib and json to call /api/chat with stream: true and print each NDJSON line as it arrived.

  2. Why it matters

    Developers who cannot or will not add the official ollama library now have a working reference for streaming output with zero extra dependencies, though they must handle model load times and history themselves.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, every morning.

Context & Analysis

The post is aimed at a common friction point: Ollama ships an official Python library, but some scripts are better off without it. By importing only urllib and json, the author avoids pip entirely, which suits single-file tools or machines where adding packages is a hassle. The script targets the local endpoint at http://127.0.0.1:11434/api/chat with a small qwen2.5:0.5b model, so the whole loop stays visible in about 20 lines.

The mechanism rests on NDJSON: with stream set to true, each line is a self-contained JSON object holding a piece of message.content and a done flag. Iterating over the response and printing each piece with flush is what produces the typewriter effect in a terminal, though redirecting to a pipe collapses it into one block. The author notes that json.loads accepts the raw bytes directly, since Python 3.6 and later detect UTF-8 automatically, sidestepping decoding pitfalls.

The caveats are practical rather than conceptual. The first request waits on model loading, measured at roughly 30 seconds for the 0.5B model, which matters if you set your own timeout. And once streaming works, conversation history and token-stat formatting become your own responsibility, so the author suggests the official library when that overhead grows; the point here is running with zero dependencies.

FAQ
What is the trick to getting streaming output?
Set stream to true in the /api/chat request; the response comes back as NDJSON, one JSON object per line. Reading each line and printing message.content with flush gives the streaming effect.
How long does the first request take?
The first request is slow because the model must load; the author measured about 30 seconds for the 0.5B model. urllib's default has no timeout, so set a long one if you add a timeout yourself.
How do you know when the stream is finished?
The final chunk carries done: true, and breaking the loop on that signal also preserves the statistics included in that last chunk.
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articleConstellation Energy tops Vistra, GE Vernova for conservative AI power bets