Runtime semiformal system: @semiformal and semi() for semantically underspecified logic
Project description
semipy
Runtime semiformal system: use the @semiformal decorator and semi() to express underspecified logic (natural-language conditions, extraction rules, ellipsis regions in methods). The first time a slot runs, an agentic pipeline (OpenRouter + pydantic_ai with tools) generates a Python function; it is validated, committed to a versioned portal, and written to a dispatch module. Later calls reuse that implementation with optional runtime verification, so routine work is not re-sent to the model.
Setup with uv
uv is used for dependency and environment management.
Install uv (if needed):
curl -LsSf https://astral.sh/uv/install.sh | sh
Create a virtual environment and install the package (from the repo root):
cd /path/to/semipy-package
uv sync
source .venv/bin/activate
Run examples:
# Set OPENAI_API_KEY in .env or environment
uv run python examples/use_sponsorship_canonicalizer.py
uv run python examples/use_contract_intelligence.py
uv run python examples/apache_log_semiformal_stages.py --stage 1
To run without activating the venv:
uv run python examples/apache_log_semiformal_stages.py --stage 1
Configuration
- OPENROUTER_API_KEY in the environment or in a
.envfile at the project root (required for generation). - Optional: E2B_API_KEY for sandboxed gist execution; without it, a subprocess fallback is used.
- Optional: SEMIPY_PIPELINE_TRACE (
1/true/yes) for full prompt, reasoning, and tool-call dumps after each generation (environment only). - Optional: SEMIPY_SESSION_SOURCE to pin portal identity for notebooks (see
CLAUDE.md). - Optional: SEMIPY_DOCUMENT_PDF_BACKEND, SEMIPY_DOCUMENT_LAYOUT_HEAVY when passing PDF paths into slots (see
CLAUDE.md). semipy.configure(...)foropenai_api_key,openai_model,validator_model,cache_dir,max_retries,use_e2b,gist_timeout,verbose,session_source, and otherSemiConfigfields. Unknown keys are ignored.
Usage
from semipy import semiformal, semi
@semiformal
def filter_errors(rows):
return [r for r in rows if semi(f"is {repr(r['level'])} an error or warning?")]
# First run: agent generates, validates, and caches a predicate (streaming output in the terminal by default).
# Later runs: load cached implementation from the dispatch module; verify when inputs change.
In @semiformal methods you can use #> comment blocks and inline #> on ... placeholders for STATEMENT_BLOCK slots, optional #< reasoning lines (see CLAUDE.md), and semi(...) for EXPRESSION slots.
See examples/use_sponsorship_canonicalizer.py, examples/use_contract_intelligence.py, and examples/apache-log-usecase.md (with examples/apache_log_semiformal_stages.py) for fuller patterns.
Artifacts and cache
- Portal:
.semiformal/{session_id}.portal.jsonstores the DAG (slots, commits, branches, refs). - Dispatch module:
.semiformal/runtime/{module_name}.semi.pyholds generated function source for import. - Deleting
.semiformal/forces regeneration (use the library’s cache-clear helpers where provided in examples).
Architecture (summary)
- Lowering (
semipy/lowering.py): Finds#>blocks, inline specs, andsemi()calls; buildsSlotSpec(includingspec_equivalence_keyfor durable meaning).#<lines are stripped for identity so annotations do not churn slot ids. - Resolution (
semipy/resolver.py,semipy/slot_resolver.py): REUSE cached implementation when equivalence and verification allow; ADAPT from a parent commit when verify fails; GENERATE when needed. Cross-call-site donor REUSE matches the same equivalence key. Runtime PDF paths can be materialized to text at the slot boundary (semipy/documents.py). - Generation (
semipy/agents/):SemiAgent.generateruns a pydantic_ai agent with tools (profile_data_and_flow,read_upstream_context,read_file_context,read_document_context,build_and_run_gist,validate_output, etc.). Output is validated (agents/validator.py) and committed (semipy/history/version_control.py). - Reactivity (
semipy/reactivity/): OptionalDependencyGraph,DataFlow, andattach_producer_flowfor downstream tracking. - Library (
semipy/library/): Optional abstraction primitives (load_library,AbstractionLibrary, …).
For slot identity, verify gates, Jupyter anchoring, STATEMENT_BLOCK typing, and #< / #> behavior, see CLAUDE.md.
Code conventions
Use from __future__ import annotations, type hints, and pathlib.Path for I/O. Follow project rules in .cursor/rules/ and CLAUDE.md (data-agnostic logic, no placeholder code, no emoji in code or docs).
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 semiformal_py-0.1.0.tar.gz.
File metadata
- Download URL: semiformal_py-0.1.0.tar.gz
- Upload date:
- Size: 134.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aab9db5e6fe540bd7997461cbec6f05b16e817ab5e7b24838fdf534090817f54
|
|
| MD5 |
bd56480dc3755e14b9ea6eea61feb35f
|
|
| BLAKE2b-256 |
659162d7f88f3719443334ae60e5453f5e0f761e38aa87d2a23d447aac048cc3
|
File details
Details for the file semiformal_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: semiformal_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 163.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af806b2728b24a67a2b6def64497138aee5de2ced67709cebb6ff3c131b24619
|
|
| MD5 |
c7d1e61ab3c926c6ebc3e717d35fe89f
|
|
| BLAKE2b-256 |
2c4b5f1fe6b2963c12b27f162ba53f60fc8dd763cc8dc42bd82dcfcb6887f0bd
|