Reliable LLM interaction with pattern matching and retries.
Project description
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.91116langchain-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.messagesandlangchain-llm7. - Message-Aware: Operates on
BaseMessagelist (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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmatch_messages-2025.9.111222.tar.gz.
File metadata
- Download URL: llmatch_messages-2025.9.111222.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
464ec1fe986763dd81b5ac6af1aa8fd0a0bc007e5c02d1b3d0a7d0ef9dd48b1a
|
|
| MD5 |
6a0009d7056317dd17ec1125f8853532
|
|
| BLAKE2b-256 |
ceb9ac5282593c9ea1c035654dc0e0f2d32042d1365ae61656fd6b7581895b09
|
File details
Details for the file llmatch_messages-2025.9.111222-py3-none-any.whl.
File metadata
- Download URL: llmatch_messages-2025.9.111222-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20dd61380b9d60e9743aeb65f2a97be75f9ef77d858f571aa22fc3e1029106cc
|
|
| MD5 |
dcad657c5df64dafe32bdd86c82db776
|
|
| BLAKE2b-256 |
fd7585c0fb104372b518596b11d87fda3fd4e58491ef520bb416161d794704e0
|