PyFuncAI
PyFuncAI is a Python library for defining functions with prompts and generating their implementations with an LLM at runtime.
The library is built around a small API:
- connect to a model provider
- describe a function in natural language
- request lazy or eager generation
- optionally cache generated source for reuse across runs
PyFuncAI currently supports local Ollama models and remote OpenAI and Gemini providers.
Installation
Install from PyPI:
pip install pyfuncai
For local development:
pip install -e .[dev]
Quick Start
Ollama
from pyfuncai import connect, create_function
connect(
"ollama",
model="qwen3.5:latest",
base_url="http://localhost:11434",
timeout=180,
)
greet = create_function(
"Return a short greeting for the provided name.",
signature="(name: str) -> str",
function_name="greet",
mode="eager",
cache=True,
)
print(greet("Alice"))
OpenAI
from pyfuncai import connect, create_function
connect(
"openai",
model="gpt-5-mini",
api_key="YOUR_OPENAI_API_KEY",
)
slugify = create_function(
"Convert text into a URL-friendly slug.",
signature="(text: str) -> str",
function_name="slugify",
)
Gemini
from pyfuncai import connect, create_function
connect(
"gemini",
model="gemini-2.5-flash",
api_key="YOUR_GEMINI_API_KEY",
)
summarize = create_function(
"Summarize a short paragraph in one sentence.",
signature="(text: str) -> str",
function_name="summarize",
)
Core Concepts
connect(provider, **config)
Registers the default provider used by subsequent create_function() calls.
Supported providers:
ollamaopenaigemini
Common configuration keys:
modeltimeoutcache_dir
Provider-specific keys:
- Ollama:
base_url - OpenAI:
api_key,base_url - Gemini:
api_key,base_url
create_function(...)
Creates a prompt-defined callable.
Common arguments:
promptsignaturefunction_namemode="lazy"ormode="eager"cache=Trueprovidercache_dirsystem_prompttemperaturemax_output_tokensallow_modules
The return value is a GeneratedFunction. It behaves like a normal callable and also exposes:
.build(force=False).source.cache_key.is_built
createFunction(...) is available as a compatibility alias.
Generation Modes
lazy: generation happens on first invocationeager: generation happens duringcreate_function()
Caching
Generated source can be cached on disk and reused across runs. Cache keys include:
- prompt
- signature
- function name
- provider identity
- mode
- allowed modules
The default cache location is OS-specific. You can override it globally with connect(..., cache_dir=...) or per function with create_function(..., cache_dir=...).
CLI
PyFuncAI ships with a small command-line interface for prebuilding functions and inspecting cached source.
Prebuild every prompt-defined function created by a script:
pyfuncai build path/to/script.py
Force regeneration even when cached source already exists:
pyfuncai build --force path/to/script.py
Inspect the cache directory:
pyfuncai cache path
pyfuncai cache list
pyfuncai cache clear
You can point cache commands at a different directory with --cache-dir.
Safety
PyFuncAI validates generated code before execution. The current implementation includes:
- AST validation
- exact signature matching
- restricted imports
- restricted builtins at execution time
This is still a code-generation library, not a hardened sandbox. Generated code should be treated as untrusted.
Development
The repository uses three long-lived branches:
main: stable development and PyPI releasespreview: prerelease validation and release-candidate testingdev: active integration branch
Automation lives under .github/workflows/:
ci.yml: formatting, tests, and package build checks onmain,preview,dev, and pull requestsrelease.yml: stable releases frommaintags such asv0.1.3prerelease.yml: GitHub prereleases frompreviewtags such asv0.1.4rc1
Run the test suite locally:
pytest
Run the optional live Ollama test:
PYFUNCAI_RUN_OLLAMA_TESTS=1 pytest tests/test_ollama_live.py
PowerShell:
$env:PYFUNCAI_RUN_OLLAMA_TESTS = "1"
pytest tests/test_ollama_live.py
Releasing
Stable releases are published to PyPI from main.
Typical stable release flow:
- Update
versioninpyproject.toml. - Commit and push to
main. - Create and push a matching tag.
Example:
git checkout main
git pull
git tag v0.1.3
git push origin main
git push origin v0.1.3
Preview releases are created from preview and published as GitHub prereleases only.
Example:
git checkout preview
git pull
git tag v0.1.4rc1
git push origin preview
git push origin v0.1.4rc1
Contributing
Contributions are welcome. A good contribution usually includes:
- a clear problem statement or use case
- tests for behavior changes
- documentation updates when the public API changes
Before opening a pull request:
python -m black src tests example.py
python -m pytest
python -m build
License
Apache-2.0
Metadata
Release files for PyFuncAI 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyfuncai-0.1.7.tar.gz | 25.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyfuncai-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 52.1 kB
Release files / pyfuncai-0.1.7.tar.gz
| Download URL | pyfuncai-0.1.7.tar.gz |
|---|---|
| Size | 25.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f2d95d6e5ddcccc30052ab52ddec98332657a766b0de72a2b8bd220e63de66b7
|
|
BLAKE2b-256 checksum How to use checksums |
7926e1b8e58214d8497a34fbeaa16f3f945d66431dbbbc679d1809a9ea5a9543
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 14, 2026.
Transparency logRelease files / pyfuncai-0.1.7-py3-none-any.whl
| Download URL | pyfuncai-0.1.7-py3-none-any.whl |
|---|---|
| Size | 26.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8cc19a2d5390e510e01cc42762861478defee0a2ef9ec80ba93e31fd69221978
|
|
BLAKE2b-256 checksum How to use checksums |
8a451f392afaf600be50fb6ec80dd5f28af098c50e3d6fdf58e575b85ffb73f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 14, 2026.
Transparency logSecurity for PyFuncAI 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pyfuncai-0.1.7.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 14, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.