Skip to main content

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.

Metadata

Release files for llmatch-messages 2025.9.131321

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmatch-messages 2025.9.131321
File Size Uploaded
llmatch_messages-2025.9.131321.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llmatch-messages 2025.9.131321
File Interpreter ABI Platform
llmatch_messages-2025.9.131321-py3-none-any.whl Python 3 none any Details

Total release size: 20.1 kB

Release files / llmatch_messages-2025.9.131321.tar.gz

Download URL llmatch_messages-2025.9.131321.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f4be72cf3c6c6491836b12a73af1926645f8653952470f70f6c005f3251be331
BLAKE2b-256 checksum
How to use checksums
739ae9888f44ef213f9ac8dbe9c23c0797e2ac0de8f074a760cecf69945d07cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / llmatch_messages-2025.9.131321-py3-none-any.whl

Download URL llmatch_messages-2025.9.131321-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
445e06f15acbae0c2a6e12687266c2cfeef8308e66e811c6fb7e8e007a1d8ad3
BLAKE2b-256 checksum
How to use checksums
3309c5098bbc41af4bde5b7ee2bca1dce7ac8046159d866194cb9daaebd6a139
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.11
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page