This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
LangDiff is a Python library that enables streaming structured LLM outputs to frontends through intelligent partial JSON parsing and automatic change tracking. The library consists of two main components:
- Streaming Parser (
src/langdiff/parser/): Handles progressive parsing of incomplete JSON structures as tokens arrive - Change Tracker (
src/langdiff/tracker/): Tracks mutations on objects and generates JSON Patch diffs for efficient frontend synchronization
This project uses uv for dependency management:
uv sync --dev # Install dependencies including dev groupuv run pytest # Run all tests
uv run pytest tests/test_parser.py # Run specific test file
uv run pytest -v # Verbose output
uv run pytest -k "test_name" # Run specific test by name patternuv run pytest --cov=langdiff --cov-fail-under=75 # Run tests with 75% coverage requirement
uv run pytest --cov=langdiff --cov-report=term-missing # Show missing lines in detail
uv run pytest --cov=langdiff # Basic coverage reportuv run ruff check # Check for linting issues
uv run ruff check --fix # Auto-fix linting issues
uv run ruff format # Format codeuv run python example.py # Run the main demo exampleuv build # Build wheel and source distributionuv sync --group docs # Install documentation dependencies
uv run mkdocs serve # Start development server at http://127.0.0.1:8000
uv run mkdocs build # Build static site to ./site directoryParser Module (src/langdiff/parser/):
model.py: Defines streaming data types (Object,List,String,Atom) that handle partial JSON parsingparser.py: Contains the mainParserclass that processes token streams and triggers callbacks- Streaming types support event callbacks (
on_append,on_complete,on_start) for real-time updates - Integrates with Pydantic through
to_pydantic()method for OpenAI SDK compatibility
Tracker Module (src/langdiff/tracker/):
change_tracker.py: Abstract base classes for change tracking (ChangeTracker,DiffBuffer)impl.py: Concrete implementations (JSONPatchChangeTracker,EfficientJSONPatchChangeTracker)changes.py: Operation definitions and change application utilities- Provides object proxying to automatically capture mutations and generate JSON Patch operations
- Event-Driven Architecture: Streaming values use callback patterns to notify consumers of updates
- Proxy Pattern: Change trackers wrap objects in proxies to intercept and record mutations
- Builder Pattern: Parsers incrementally build structured data as tokens arrive
- Type Safety: Heavy use of generics and type hints for compile-time safety
- OpenAI SDK: LangDiff models can be converted to Pydantic models via
to_pydantic()for use with structured output APIs - JSON Patch: Change tracking generates RFC 6902 compliant JSON Patch operations (with custom
appendoperation for efficiency) - Pydantic: Seamless interop with existing Pydantic-based codebases
- Tests are organized by module:
test_parser.py,test_change_tracker.py,test_partial_json.py - Focus on streaming behavior, partial parsing scenarios, and change tracking accuracy
- Example-driven testing using realistic LLM output patterns
Core dependencies:
jiter: Fast JSON parsingjsonpatch: JSON Patch operationsjsonpointer: JSON Pointer implementationpydantic: Data validation and serialization
Development dependencies:
pytest: Testing frameworkruff: Linting and formattingopenai: For examples and integration testing
Documentation dependencies:
mkdocs: Static site generatormkdocs-material: Material Design theme for MkDocsmkdocstrings[python]: Auto-generate API docs from docstrings