The ultimate Python toolkit for X12 EDI processing
Project description
██╗ ██╗ ██╗██████╗ ███████╗██████╗ ██╗
╚██╗██╔╝███║╚════██╗ ██╔════╝██╔══██╗██║
╚███╔╝ ╚██║ █████╔╝ █████╗ ██║ ██║██║
██╔██╗ ██║██╔═══╝ ██╔══╝ ██║ ██║██║
██╔╝ ██╗ ██║███████╗ ███████╗██████╔╝██║
╚═╝ ╚═╝ ╚═╝╚══════╝ ╚══════╝╚═════╝ ╚═╝
P Y T H O N T O O L K I T
The ultimate Python toolkit for X12 EDI processing
Parse, validate, and generate healthcare & supply chain transactions with HIPAA compliance
Architecture
flowchart TB
subgraph Input["📄 Input"]
EDI[("EDI File\n837/835/850...")]
end
subgraph Core["⚙️ Core Engine"]
direction TB
TOK[Tokenizer] --> PARSE[Parser]
PARSE --> LOOP[Loop Builder]
LOOP --> VAL[Validator]
end
subgraph Schema["📋 Schema & Codes"]
direction TB
SCH[(Transaction\nSchemas)]
CODE[(Code\nSets)]
end
subgraph Output["📤 Output"]
direction TB
MODEL[Pydantic Models]
ACK[997/999 Acks]
GEN[EDI Generator]
end
EDI --> TOK
SCH -.-> VAL
CODE -.-> VAL
VAL --> MODEL
MODEL --> ACK
MODEL --> GEN
style Input fill:#e1f5fe
style Core fill:#fff3e0
style Schema fill:#f3e5f5
style Output fill:#e8f5e9
Features
- Parsing - Parse X12 EDI documents with automatic delimiter detection
- Validation - Multi-level validation with HIPAA compliance checks
- Generation - Generate properly formatted X12 EDI output
- Streaming - Memory-bounded streaming for large files
- Acknowledgments - Generate 997/999 functional acknowledgments
- Schema Support - Schema-based validation for 837, 835, 270/271, 850
- Code Validation - NPI, Tax ID, ICD-10, CPT/HCPCS validation
- Trading Partners - Partner configuration management
Installation
pip install x12-python
Or install from source:
git clone https://github.com/copyleftdev/x12-python.git
cd x12-python
pip install -e .
Quick Start
Parse an EDI File
from x12.core.parser import Parser
# Parse EDI content
parser = Parser()
interchange = parser.parse(edi_content)
# Access parsed data
print(f"Sender: {interchange.sender_id}")
print(f"Receiver: {interchange.receiver_id}")
for group in interchange.functional_groups:
for transaction in group.transaction_sets:
print(f"Transaction: {transaction.transaction_set_id}")
Validate a Transaction
from x12.core.validator import X12Validator
validator = X12Validator()
report = validator.validate(interchange)
if report.is_valid:
print("Document is valid!")
else:
for error in report.errors:
print(f"Error: {error}")
Generate EDI Output
from x12.core.generator import Generator
from x12.models import Segment, Element
generator = Generator()
# Generate a segment
segment = Segment(
segment_id="NM1",
elements=[
Element(value="85", index=1),
Element(value="2", index=2),
Element(value="ACME HOSPITAL", index=3),
]
)
edi_output = generator.generate(segment)
# Output: NM1*85*2*ACME HOSPITAL~
Stream Large Files
from x12.streaming import StreamingSegmentReader
# Process large files with bounded memory
with open("large_file.edi") as f:
reader = StreamingSegmentReader(f)
for segment in reader.segments():
process(segment)
Generate Acknowledgments
from x12.acknowledgments import AcknowledgmentGenerator
generator = AcknowledgmentGenerator(
sender_id="RECEIVER",
receiver_id="SENDER",
)
# Generate 997 acknowledgment
ack = generator.generate_997(functional_group)
Supported Transactions
Healthcare (HIPAA 5010)
| Transaction | Version | Description |
|---|---|---|
| 837P | 005010X222A1 | Health Care Claim: Professional |
| 837I | 005010X223A3 | Health Care Claim: Institutional |
| 837D | 005010X224A3 | Health Care Claim: Dental |
| 835 | 005010X221A1 | Health Care Claim Payment/Advice |
| 270/271 | 005010X279A1 | Eligibility Benefit Inquiry/Response |
| 276/277 | 005010X212 | Claim Status Request/Response |
| 834 | 005010X220A1 | Benefit Enrollment and Maintenance |
| 278 | 005010X217 | Health Care Services Review (Prior Auth) |
| 820 | 005010X218 | Premium Payment |
Supply Chain (4010)
| Transaction | Description |
|---|---|
| 850 | Purchase Order |
| 855 | Purchase Order Acknowledgment |
| 856 | Ship Notice/Manifest (ASN) |
| 860 | Purchase Order Change |
| 810 | Invoice |
Project Structure
x12/
├── core/ # Parser, validator, generator, tokenizer
├── models/ # Segment, Element, Loop, Envelope models
├── schema/ # Schema definitions and loader
├── codes/ # Code set registry and validators
├── streaming/ # Memory-bounded streaming reader
├── acknowledgments/# 997/999 acknowledgment generation
├── trading_partners/ # Partner configuration
└── transactions/ # Transaction-specific models
├── healthcare/ # 837, 835, 270/271 models
└── supply_chain/ # 850, 856, 810 models
Testing
# Run all tests
pytest tests/
# Run with coverage
pytest tests/ --cov=x12 --cov-report=term-missing
# Run specific test types
pytest tests/unit -v
pytest tests/integration -v
pytest tests/property -v
pytest tests/compliance -v
pytest tests/performance -v
Code Quality
# Type checking
mypy x12 --strict
# Linting
ruff check .
ruff format .
Development
This project follows Test-Driven Development (TDD):
- Write failing tests first (RED)
- Implement minimal code to pass (GREEN)
- Refactor while keeping tests green (REFACTOR)
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Quick start:
# Clone and setup
git clone https://github.com/donjohnson/x12-edi-tools.git
cd x12-edi-tools
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
# Run tests
pytest tests/
API Reference
Core Classes
Parser- Parse EDI content into structured objectsGenerator- Generate EDI output from objectsX12Validator- Validate EDI documentsTokenizer- Low-level EDI tokenizationDelimiters- Delimiter detection and configuration
Models
Segment- X12 segment with elementsElement- Individual data elementLoop- Hierarchical loop structureInterchange- ISA/IEA envelopeFunctionalGroup- GS/GE envelopeTransactionSet- ST/SE envelope
Schema
SchemaLoader- Load schema definitionsTransactionSchema- Complete transaction schemaSegmentDefinition- Segment structure definitionElementDefinition- Element validation rules
Codes
CodeRegistry- Registry of code setsCodeSet- Set of valid codes with descriptionsvalidate_npi()- NPI validation with Luhn checkvalidate_tax_id()- EIN validationvalidate_diagnosis_code()- ICD-10 format validationvalidate_procedure_code()- CPT/HCPCS validation
Trading Partners
TradingPartner- Partner configurationPartnerRegistry- Partner managementContactInfo- Contact information
Changelog
See CHANGELOG.md for version history and release notes.
License
MIT License - see LICENSE for details.
Copyright (c) 2024 Don Johnson
Acknowledgments
Built following X12 standards from ASC X12 and HIPAA implementation guides.
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 x12_python-0.1.0.tar.gz.
File metadata
- Download URL: x12_python-0.1.0.tar.gz
- Upload date:
- Size: 88.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbd9cfaa67095b38d01cc5d09952b42d2800363275ab61b026f36dd66fc12d87
|
|
| MD5 |
99fc8a8cedb3a7d498dc2a43d2cc962d
|
|
| BLAKE2b-256 |
63fa4f8e4ef32800dedbe11f5f8dd8ed5ea14ff085062d61b168a0fe0af792ba
|
File details
Details for the file x12_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: x12_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 55.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d7ba7b8f2c3d70c8fd9245a86e7586527bd42784513d8840ee74a15ad7f4d6a
|
|
| MD5 |
c3f40f51bb0853e6401ba180a33783a3
|
|
| BLAKE2b-256 |
ddc02685f2bc1bd3cf8cda66193a7b9f1b2e897b0afae366b99979301b0b1d12
|