Skip to main content

Prompt-first declarative HTTP framework on top of FastAPI and PydanticAI

Project description

yapi

PyPI Python License: MIT

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 unhyphenated yapi was taken by a 2018 project). Import path is still yapi.

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:

  1. parses the request body into the BaseModel you declared as a parameter,
  2. calls your function synchronously to optionally produce a dynamic prompt (the function's return value, must be None or str),
  3. composes the final system prompt from: response-model docstring + function docstring + dynamic prompt,
  4. invokes the configured agent_runner (defaulting to a PydanticAI Agent) with the prompt + request payload,
  5. validates the agent's output against your return annotation and serializes via FastAPI.

Contract (hard rules)

  • PromptRouter subclasses fastapi.APIRouter and overrides .get/.post/.put/.patch/.delete. All routes on a PromptRouter are 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 BaseModel subclass.
  • At most one parameter may be a BaseModel (the request body). Other parameters must have default=fastapi.Depends(...).
  • Function body must return either None or a str (the dynamic prompt). Anything else raises at request time. async def is 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

pyyapi-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyyapi-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

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

Hashes for pyyapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d819eaf4b7870b8cf5015a3b35e5ed73f9a2318d3576679a3680330d4580aeeb
MD5 7a8139645630b8f505d6658dbafc8baa
BLAKE2b-256 7272f64a5cd2b8bd27d49e02af2a7e92f9962abc13bd5f0d1f8ecae210e26696

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyyapi-0.1.0.tar.gz:

Publisher: release.yml on TokenRollAI/yapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pyyapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 548f322477d0ef652b3ae23023e3bc763eff546df1d3ed5624d1d386be71ffc4
MD5 dcca0f5f144f2ecae22b7b4aa6f96922
BLAKE2b-256 52c29d482b337f02669ab7855d487f0bb8694bebfbe28d609fa4cd7c0a57b222

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyyapi-0.1.0-py3-none-any.whl:

Publisher: release.yml on TokenRollAI/yapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page