Skip to main content

Reliable LLM interaction with pattern matching and retries.

Project description

PyPI version License: Apache-2.0 Downloads LinkedIn

llmatch-messages

llmatch-messages is a robust utility for pattern-verified LLM interactions built on top of langchain-llm7 and langchain-core. It enables structured, retryable conversations with LLMs by matching regex patterns in their responses, ideal for use cases where consistency and format are crucial — such as parsing XML/JSON-style tags, code blocks, or key phrases.

This package is production-oriented, supports retries with exponential backoff, and provides rich diagnostics when responses do not match expectations.


🔧 Installation

pip install llmatch-messages

This will also install:

  • langchain-llm7==2025.05.91116
  • langchain-core==0.3.51

✨ Example

from llmatch_messages import llmatch
from langchain_llm7 import ChatLLM7
from langchain_core.messages import HumanMessage, SystemMessage

llm = ChatLLM7()

response = llmatch(
    llm=llm,
    messages=[
        SystemMessage(content="You are a helpful assistant. Write the output in the format: <image_desc>...</image_desc>"),
        HumanMessage(
            content=[
                {"type": "text", "text": "Describe this image:"},
                {"type": "image_url", "image_url": {"url": "https://llm7.io/logo.png"}},
            ],
        )
    ],
    pattern=r"<image_desc>\s*(.*?)\s*</image_desc>",
    verbose=True,
)

if response["success"]:
    print("Extracted:", response["extracted_data"])
else:
    print("Error:", response["error_message"])

🧠 Key Features

  • Pattern Matching: Use regex to validate and extract structured parts of LLM responses.
  • Retry Logic: Automatically retries if output does not conform, using exponential backoff.
  • LangChain Native: Works seamlessly with langchain_core.messages and langchain-llm7.
  • Message-Aware: Operates on BaseMessage list (e.g. HumanMessage, SystemMessage).
  • Detailed Diagnostics: Verbose mode traces all steps and decision points.
  • Fail-Safe: Handles malformed LLM responses gracefully and provides fallback messaging.

🧪 Return Format

The function returns a dictionary:

{
    "success": True | False,
    "extracted_data": Optional[List[str]],
    "final_content": Optional[str],
    "retries_attempted": int,
    "error_message": Optional[str],
    "raw_response": Optional[Any],
}

🪄 Common Use Cases

  • Validate XML-like or markdown-formatted LLM outputs.
  • Parse code blocks (```...```), JSON sections, or tags.
  • Build structured LLM workflows that require machine-readable responses.

📄 License

Licensed under the Apache License 2.0.


🙋‍♂️ Author

Developed by Eugene Evstafev, software developer at the University of Cambridge, creator of llm7.io.

For feedback or contributions, feel free to open an issue or PR on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llmatch_messages-2025.5.91122.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llmatch_messages-2025.5.91122-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file llmatch_messages-2025.5.91122.tar.gz.

File metadata

  • Download URL: llmatch_messages-2025.5.91122.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.11

File hashes

Hashes for llmatch_messages-2025.5.91122.tar.gz
Algorithm Hash digest
SHA256 c8bfaebf37ab8f7f630c0690956801f25641f3f4f499bd0dab727e8c604a5b20
MD5 a1e3113915b06af36a053d07b068c2f2
BLAKE2b-256 64b0202d1c1127c19ac8eb4218b8c769b5474f4d757bfe4758cb90589586416d

See more details on using hashes here.

File details

Details for the file llmatch_messages-2025.5.91122-py3-none-any.whl.

File metadata

File hashes

Hashes for llmatch_messages-2025.5.91122-py3-none-any.whl
Algorithm Hash digest
SHA256 ac4b74fd4e76b4ba911c0e071f42da5c387d80cfba86acf8b535834467b68f31
MD5 909ebc387d19dbb50f03be3a9b74c03e
BLAKE2b-256 1ac0fdeec48fcd35e5587b2f91ac7dacc7e8649585d95959c2fd454cdce67a87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page