Prompt-first declarative HTTP framework on top of FastAPI and PydanticAI
Project description
yapi
Prompt-first declarative HTTP framework — write a normal Python function with a docstring, get an LLM-powered HTTP endpoint with structured JSON responses.
yapi is a thin layer on top of FastAPI and PydanticAI. You declare an HTTP route by decorating a regular function; the framework takes the function signature, the response model's docstring, the function's docstring and any dynamic prompt it returns, composes them into a system prompt, and hands the result to a PydanticAI Agent to produce a validated BaseModel response.
Package name on PyPI is
pyyapi(the unhyphenatedyapiwas taken by a 2018 project). Import path is stillyapi.
Install
pip install pyyapi
Python 3.12+ required.
Quick start
from fastapi import FastAPI
from pydantic import BaseModel
from yapi import PromptRouter
class WishIn(BaseModel):
user_id: str
wish: str
class WishOut(BaseModel):
"""You are a wish-granting entity. Decide whether to grant the wish."""
granted: bool
message: str
app = FastAPI(title="yapi showcase")
router = PromptRouter()
@router.post("/wish")
def make_a_wish(req: WishIn) -> WishOut:
"""Decide whether to grant the user's wish."""
app.include_router(router)
Run it:
YAPI_MODEL=test uvicorn examples.wish_api:app --reload
YAPI_MODEL=test activates PydanticAI's built-in TestModel — no API key, no network, perfect for offline smoke tests. For real models, set e.g. YAPI_MODEL=openai:gpt-4o or YAPI_MODEL=anthropic:claude-3-5-sonnet.
Open http://localhost:8000/docs for the auto-generated OpenAPI UI.
How it works
For each request, yapi:
- parses the request body into the
BaseModelyou declared as a parameter, - calls your function synchronously to optionally produce a dynamic prompt (the function's
returnvalue, must beNoneorstr), - composes the final system prompt from: response-model docstring + function docstring + dynamic prompt,
- invokes the configured
agent_runner(defaulting to a PydanticAIAgent) with the prompt + request payload, - validates the agent's output against your return annotation and serializes via FastAPI.
Contract (hard rules)
PromptRoutersubclassesfastapi.APIRouterand overrides.get/.post/.put/.patch/.delete. All routes on aPromptRouterare prompt routes — don't mix plain FastAPI routes onto it.- The decorator only accepts the path argument. FastAPI kwargs like
tags=,summary=,status_code=,response_class=,response_model=are silently ignored. - Return annotation must be a
BaseModelsubclass. - At most one parameter may be a
BaseModel(the request body). Other parameters must havedefault=fastapi.Depends(...). - Function body must
returneitherNoneor astr(the dynamic prompt). Anything else raises at request time.async defis not supported.
Violations are raised as YapiDeclarationError at decoration time — broken routes fail at import, not at request time.
Dependency injection
from fastapi import Depends
def get_db():
...
@router.post("/wish")
def make_a_wish(req: WishIn, db = Depends(get_db)) -> WishOut:
"""..."""
return f"user has {db.balance(req.user_id)} wishes left"
Custom agent runner
PromptRouter(agent_runner=...) accepts any callable with the signature (*, prompt, request, injected, response_model) -> dict. Useful for tests:
router = PromptRouter(
agent_runner=lambda **_: {"granted": True, "message": "ok"},
)
Development
uv sync --extra dev
uv run pytest
uv run uvicorn examples.wish_api:app --reload
License
MIT
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 pyyapi-0.1.0.tar.gz.
File metadata
- Download URL: pyyapi-0.1.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d819eaf4b7870b8cf5015a3b35e5ed73f9a2318d3576679a3680330d4580aeeb
|
|
| MD5 |
7a8139645630b8f505d6658dbafc8baa
|
|
| BLAKE2b-256 |
7272f64a5cd2b8bd27d49e02af2a7e92f9962abc13bd5f0d1f8ecae210e26696
|
Provenance
The following attestation bundles were made for pyyapi-0.1.0.tar.gz:
Publisher:
release.yml on TokenRollAI/yapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyyapi-0.1.0.tar.gz -
Subject digest:
d819eaf4b7870b8cf5015a3b35e5ed73f9a2318d3576679a3680330d4580aeeb - Sigstore transparency entry: 1615324157
- Sigstore integration time:
-
Permalink:
TokenRollAI/yapi@307836a22dfb774b2841663e40a0381a213972b9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/TokenRollAI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@307836a22dfb774b2841663e40a0381a213972b9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyyapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyyapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
548f322477d0ef652b3ae23023e3bc763eff546df1d3ed5624d1d386be71ffc4
|
|
| MD5 |
dcca0f5f144f2ecae22b7b4aa6f96922
|
|
| BLAKE2b-256 |
52c29d482b337f02669ab7855d487f0bb8694bebfbe28d609fa4cd7c0a57b222
|
Provenance
The following attestation bundles were made for pyyapi-0.1.0-py3-none-any.whl:
Publisher:
release.yml on TokenRollAI/yapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyyapi-0.1.0-py3-none-any.whl -
Subject digest:
548f322477d0ef652b3ae23023e3bc763eff546df1d3ed5624d1d386be71ffc4 - Sigstore transparency entry: 1615324163
- Sigstore integration time:
-
Permalink:
TokenRollAI/yapi@307836a22dfb774b2841663e40a0381a213972b9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/TokenRollAI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@307836a22dfb774b2841663e40a0381a213972b9 -
Trigger Event:
push
-
Statement type: