expectllm: An “expect”-style framework for scripting LLM conversations (365 lines)

Showcase

What My Project Does

I built a small library called expectllm.

It treats LLM conversations like classic expect scripts:

send → pattern match → branch

You explicitly define what response format you expect from the model.
If it matches, you capture it.
If it doesn’t, it fails fast.

Example:

from expectllm import Conversation

c = Conversation()
c.send("Review this code for security issues")
c.expect(r"found (\d+) issues")

if int(c.match.group(1)) > 0:
c.send("Fix the top 3")

Core features:

  • expect_json(), expect_number(), expect_yesno()
  • Regex pattern matching with capture groups
  • Auto-generates format instructions from patterns
  • Works with OpenAI and Anthropic (any compatible API)
  • ~365 lines of code, fully readable
  • Full type hints

Repo: https://github.com/entropyvector/expectllm

PyPI: https://pypi.org/project/expectllm/

Target Audience

This is intended for:

  • Developers who want deterministic LLM scripting
  • People who find full agent frameworks too heavy for simple workflows
  • Engineers who prefer explicit response contracts
  • Prototyping and production systems where predictable branching is important

It is not designed to replace full orchestration frameworks.
It focuses on minimalism and control.

Comparison

Most LLM frameworks provide:

  • Tool orchestration
  • Memory systems
  • Multi-agent abstractions
  • Complex pipelines

expectllm intentionally does not.

Instead, it focuses on:

  • Explicit pattern matching
  • Deterministic branching
  • Minimal abstraction
  • Transparent control flow

It’s closer in spirit to expect for terminal automation than to full agent frameworks.

Would appreciate feedback:

  • Is this approach useful in real-world projects?
  • What edge cases should I handle?
  • Where would this break down?

Posted Feb 20, 2026, 4:29 PM UTC

05
Loading comments…