A type-safe graph execution framework built on top of OpenLit for LLM pipelines
Project description
Hapax: Type-Safe Graph Execution Framework
Hapax is a powerful Python framework for building type-safe, observable data processing pipelines. Built on top of OpenLit, it provides multi-stage type checking, rich error messages, and comprehensive monitoring out of the box.
Features
✨ Multi-Stage Type Safety
- Import-time type validation through
@opsdecorator - Definition-time type checking when building graphs
- Runtime type validation during execution
- Rich error messages that pinpoint issues
🔍 Static Analysis
- Graph structure validation
- Cycle detection
- Type compatibility verification
- Configuration and metadata checks
📊 OpenLit Integration
- Automatic monitoring and observability
- Execution time tracking
- Success/failure rates
- Graph visualization
🎮 Intuitive API
- Fluent interface for building pipelines
- Type-safe operation composition using
>> - Rich control flow (branch, merge, condition, loop)
Quick Start
- Install Hapax:
pip install hapax
- Create your first pipeline:
from hapax import ops, graph
import openlit
from typing import List, Dict
# Initialize OpenLit (optional but recommended)
openlit.init(otlp_endpoint="http://127.0.0.1:4318")
# Define operations - type checked at import time
@ops(name="clean_text")
def clean_text(text: str) -> str:
return text.lower().strip()
@ops(name="tokenize")
def tokenize(text: str) -> List[str]:
return text.split()
@ops(name="analyze")
def analyze(tokens: List[str]) -> Dict[str, int]:
from collections import Counter
return dict(Counter(tokens))
# Build pipeline - type compatibility checked at definition time
pipeline = (
Graph("text_processing")
.then(clean_text) # str -> str
.then(tokenize) # str -> List[str]
.then(analyze) # List[str] -> Dict[str, int]
)
# Execute pipeline - types checked at runtime
result = pipeline.execute("Hello World! Hello Hapax!")
Core Concepts
1. Operations
Operations are pure functions with multi-stage type checking:
@ops(name="summarize", tags=["nlp"])
def summarize(text: str) -> str:
"""Generate a concise summary."""
return summary
# Type checking happens at:
# 1. Import time - through @ops decorator
# 2. Definition time - when used in a graph
# 3. Runtime - during execution
result = summarize(42) # Runtime TypeError: Expected str, got int
2. Graph Building
Build complex pipelines with immediate type validation:
# Using the fluent API - type compatibility checked at definition time
pipeline = (
Graph("text_analysis")
.then(clean_text) # str -> str
.branch(
summarize, # str -> str
sentiment_analysis # str -> float
)
.merge(combine_results)
)
# Or using the >> operator for composition
pipeline = clean_text >> tokenize >> analyze # Type compatibility checked immediately
3. Control Flow
Rich control flow operations with type safety:
# Parallel Processing
pipeline = (
Graph("parallel_nlp")
.branch(
summarize, # Branch 1: str -> str
extract_entities, # Branch 2: str -> List[str]
analyze_sentiment # Branch 3: str -> float
)
.merge(lambda results: {
"summary": results[0],
"entities": results[1],
"sentiment": results[2]
})
)
# Conditional Logic
pipeline = (
Graph("smart_translate")
.then(detect_language)
.condition(
lambda lang: lang != "en",
translate_to_english, # If true
lambda x: x # If false (pass through)
)
)
OpenLit Integration
Hapax is built on OpenLit for automatic monitoring:
# 1. Basic Setup
import openlit
openlit.init(otlp_endpoint="http://localhost:4318")
# 2. Operation-Level Monitoring
@ops(
name="tokenize",
tags=["nlp"],
openlit_config={
"trace_content": True,
"disable_metrics": False
}
)
def tokenize(text: str) -> List[str]:
return text.split()
# 3. Graph-Level Monitoring
@graph(
name="nlp_pipeline",
description="Process text using NLP"
)
def process_text(text: str) -> Dict[str, Any]:
return clean >> analyze
Error Handling
Hapax provides clear error messages:
# Type Mismatch
TypeError: Cannot compose operations: output type List[str] does not match input type Dict[str, Any]
# Structural Issues
GraphValidationError: Graph contains cycles: [['op1', 'op2', 'op1']]
# Runtime Errors
BranchError: Errors in branches: [('sentiment', ValueError('Invalid input'))]
Best Practices
-
Type Safety
- Always specify input and output types
- Let Hapax handle type validation
- Use mypy for additional static checking
-
Operation Design
- Keep operations pure and focused
- Use meaningful names
- Add proper documentation
-
Monitoring
- Initialize OpenLit early
- Add meaningful tags
- Use trace_content for debugging
-
Error Handling
- Handle branch errors appropriately
- Check partial results in case of failures
- Use the rich error information
Documentation
The documentation is organized into the following sections:
-
Getting Started
- Installation Guide - Requirements and installation options
- Quick Start Guide - Get up and running in under 5 minutes
- Comprehensive Guide - In-depth explanation of all key concepts
- Examples - Code examples demonstrating various features
-
Core Features
- Graph API Reference - Complete reference for the Graph API and building pipelines
- API Reference - Comprehensive reference for all Hapax classes and functions
-
Integrations
- OpenLit Basics - Simple monitoring setup with OpenLit
- Advanced OpenLit Integration - GPU monitoring, evaluations, and advanced features
-
Advanced Features
- Evaluation Decorators - Using evaluation decorators for content safety
-
Support and Troubleshooting
- Troubleshooting Guide - Solutions for common issues
For a complete overview, start with the documentation index.
Hapax Documentation Assistant (MCP)
The Hapax Documentation Assistant is an MCP (Model Context Protocol) server that provides AI-powered access to Hapax documentation and source code through tools that can be used by AI assistants like Claude in Cursor.
Features
- 🔍 Smart Documentation Search: Find relevant documentation based on natural language queries
- 📚 Source Code Navigation: Explore and understand Hapax source code
- 🛠️ Implementation Guidance: Get guidance on implementing specific patterns or features
- 🔧 Troubleshooting: Get help with errors and issues
- 📖 Topic Exploration: Comprehensive exploration of Hapax concepts and topics
Installation
The Documentation Assistant is included in the main Hapax repository:
-
Clone the Hapax repository:
git clone https://github.com/teilomillet/hapax-py.git cd hapax-py
-
Install dependencies:
uv install -e .
Running the server
You can run the documentation server and point it to any Hapax project (including this one):
# To use with the current repository
python hapax_docs_server.py run
# To use with another Hapax project
HAPAX_DOCS_DIR=/path/to/other-project/docs HAPAX_SOURCE_DIR=/path/to/other-project/src python hapax_docs_server.py run
For development and testing, you can use MCP Inspector:
# For the current repository
python -m mcp dev hapax_docs_server.py
# For another project
HAPAX_DOCS_DIR=/path/to/other-project/docs HAPAX_SOURCE_DIR=/path/to/other-project/src python -m mcp dev hapax_docs_server.py
Installing in Cursor
To use the Hapax Documentation Assistant in Cursor:
- Go to Cursor Settings > Features > MCP
- Click the + Add New MCP Server button
- Configure as follows:
- Type: CLI (stdio transport)
- Name: Hapax Documentation Assistant
- Command:
# To use with the hapax-py repository python /path/to/hapax-py/hapax_docs_server.py run # To use with another project HAPAX_DOCS_DIR=/path/to/other-project/docs HAPAX_SOURCE_DIR=/path/to/other-project/src python /path/to/hapax-py/hapax_docs_server.py run
Path Requirements
When configuring the server in Cursor:
- Use absolute paths for the server script and any external project directories
- You can create a shell script wrapper if you need more complex environment setup:
Example wrapper script (hapax_docs_helper.sh):
#!/bin/bash
# Path to the hapax-py repository
HAPAX_ASSISTANT_PATH=/path/to/hapax-py
# Optional: Point to another project
# export HAPAX_DOCS_DIR=/path/to/other-project/docs
# export HAPAX_SOURCE_DIR=/path/to/other-project/src
# Run the assistant
python $HAPAX_ASSISTANT_PATH/hapax_docs_server.py run
Then in Cursor, set the command to: /path/to/hapax_docs_helper.sh
Project-Specific Configuration
For project-specific configuration in Cursor, create a .cursor/mcp.json file in your project:
{
"mcpServers": {
"hapax-docs": {
"command": "python",
"args": ["/absolute/path/to/hapax-py/hapax_docs_server.py", "run"]
}
}
}
Or to use with the current project directory:
{
"mcpServers": {
"hapax-docs": {
"command": "bash",
"args": ["-c", "HAPAX_DOCS_DIR=./docs HAPAX_SOURCE_DIR=./src python /absolute/path/to/hapax-py/hapax_docs_server.py run"]
}
}
}
Available Tools
The Hapax Documentation Assistant provides the following tools:
- search_docs: Search the Hapax documentation and source code
- get_section_content: Get detailed information about a documentation section
- get_implementation_pattern: Get guidance on implementing specific patterns
- get_source_element: View source code for specific elements (functions, classes, etc.)
- find_usage_examples_tool: Find examples of how components are used in the codebase
- get_implementation_guidance: Get guidance on implementing features or components
- troubleshoot_issue: Get help with errors or issues
- understand_hapax_component: Get comprehensive information about a Hapax component
- explore_hapax_topic: Explore general topics in the Hapax framework
Using the Tools in Agent
Once added, the Hapax Documentation Assistant will be available to the Agent in Cursor's Composer. You can:
- Open Composer in Cursor
- Ask questions about Hapax documentation, for example:
- "How do I use the Graph component in Hapax?"
- "Search for documentation about operations in Hapax"
- "Show me an example of implementing error handling in Hapax"
The Agent will automatically use your MCP tools when relevant. You can also directly prompt tool usage by mentioning a specific tool:
- "Use the search_docs tool to find information about error handling in Hapax"
License
MIT License - see LICENSE for details.
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
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 hapax-0.1.5.tar.gz.
File metadata
- Download URL: hapax-0.1.5.tar.gz
- Upload date:
- Size: 23.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd66d7b607b3bdd84bee9fb3cb56fa1c0f86c6d5d395f279eab513a8cb0894e9
|
|
| MD5 |
9c8a48417f8b0212c420b5ec48f98282
|
|
| BLAKE2b-256 |
622bdd20e8953317cf99040cc33e089d2bfaeea97c691651b60828b188384428
|
File details
Details for the file hapax-0.1.5-py3-none-any.whl.
File metadata
- Download URL: hapax-0.1.5-py3-none-any.whl
- Upload date:
- Size: 59.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e31992d2b8361d91f11b010c669fb3d4d087454e23ec6b98fac489136c57e1c
|
|
| MD5 |
c951005b14f26ab69a32eefcfe59dcab
|
|
| BLAKE2b-256 |
4efd9c712f0e179b4daa41d710c9ecddd6ee79802f65db0b8ef6ac167cf69133
|