Prompt-defined Python functions with lazy or eager LLM-backed generation.
Project description
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
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 pyfuncai-0.1.7.tar.gz.
File metadata
- Download URL: pyfuncai-0.1.7.tar.gz
- Upload date:
- Size: 25.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2d95d6e5ddcccc30052ab52ddec98332657a766b0de72a2b8bd220e63de66b7
|
|
| MD5 |
52927c0afd38a4dd765b378818376434
|
|
| BLAKE2b-256 |
7926e1b8e58214d8497a34fbeaa16f3f945d66431dbbbc679d1809a9ea5a9543
|
Provenance
The following attestation bundles were made for pyfuncai-0.1.7.tar.gz:
Publisher:
release.yml on AaronCreor/PyFuncAI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyfuncai-0.1.7.tar.gz -
Subject digest:
f2d95d6e5ddcccc30052ab52ddec98332657a766b0de72a2b8bd220e63de66b7 - Sigstore transparency entry: 1104686950
- Sigstore integration time:
-
Permalink:
AaronCreor/PyFuncAI@13c22aea10b9b540671bb37bfdaaf40931338c2c -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/AaronCreor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@13c22aea10b9b540671bb37bfdaaf40931338c2c -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyfuncai-0.1.7-py3-none-any.whl.
File metadata
- Download URL: pyfuncai-0.1.7-py3-none-any.whl
- Upload date:
- Size: 26.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cc19a2d5390e510e01cc42762861478defee0a2ef9ec80ba93e31fd69221978
|
|
| MD5 |
9784902e5cb02e3a9261f8ef4da51d80
|
|
| BLAKE2b-256 |
8a451f392afaf600be50fb6ec80dd5f28af098c50e3d6fdf58e575b85ffb73f6
|
Provenance
The following attestation bundles were made for pyfuncai-0.1.7-py3-none-any.whl:
Publisher:
release.yml on AaronCreor/PyFuncAI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyfuncai-0.1.7-py3-none-any.whl -
Subject digest:
8cc19a2d5390e510e01cc42762861478defee0a2ef9ec80ba93e31fd69221978 - Sigstore transparency entry: 1104687013
- Sigstore integration time:
-
Permalink:
AaronCreor/PyFuncAI@13c22aea10b9b540671bb37bfdaaf40931338c2c -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/AaronCreor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@13c22aea10b9b540671bb37bfdaaf40931338c2c -
Trigger Event:
push
-
Statement type: