Lightweight structured output runtime for Python LLM calls.
Project description
structured-llm
一个轻量的 Python 结构化输出运行时。
它直接使用 Pydantic 类型作为 schema,不需要 .baml 文件、CLI、代码生成,也不需要额外的运行时编译器。默认行为是 BAML 风格的「output format prompt + 本地 JSON 提取/轻量修复 + Pydantic 校验」,因此不依赖特定供应商是否支持 response_format。
安装依赖
普通运行依赖:
uv sync --no-config --default-index https://pypi.org/simple
开发依赖安装在 dev dependency group 中,包括 pytest、pytest-asyncio、ruff、mypy:
uv sync --group dev --no-config --default-index https://pypi.org/simple
如果需要新增开发工具依赖:
uv add <package> --group dev --no-config --default-index https://pypi.org/simple
使用示例
from pydantic import BaseModel, Field
from structured_llm import StructuredClient
class Receipt(BaseModel):
merchant: str = Field(description="商户或店铺名称")
total: float = Field(description="收据最终支付总金额")
client = StructuredClient(model="gpt-4o-mini", debug=True)
receipt = client.run("Extract the receipt: Coffee $4.50", Receipt)
print(receipt.merchant)
print(receipt.total)
默认 OpenAI-compatible provider 会从 OPENAI_API_KEY 和 OPENAI_BASE_URL 读取配置;如果代码里显式传入 api_key 或 base_url,会优先使用显式参数。examples/receipt_extraction.py 会通过 python-dotenv 自动加载本地 .env。
Field(description=...) 会渲染到默认 output format prompt。debug=True 会把传给 OpenAI-compatible SDK 的 request payload 和模型解析前的原始输出打印到 stderr。默认不会发送 response_format;只有显式设置 mode="native" 或 mode="auto" 时才会尝试 provider-native structured output。
只解析已有的 LLM 文本输出:
raw = """
```json
{"merchant": "Coffee Shop", "total": 4.5}
"""
receipt = client.parse(raw, Receipt)
## 开发命令
运行测试:
```bash
PYTHONDONTWRITEBYTECODE=1 uv run --no-config --default-index https://pypi.org/simple --group dev python -m pytest -p no:cacheprovider
运行 Ruff:
uv run --no-config --default-index https://pypi.org/simple --group dev ruff check .
运行 mypy:
uv run --no-config --default-index https://pypi.org/simple --group dev mypy structured_llm
当前范围
- 支持同步调用:
StructuredClient.run(...) - 支持异步调用:
StructuredClient.arun(...) - 支持本地解析:
StructuredClient.parse(...) - 支持 Pydantic
BaseModel、list[...]、dict[...]、Literal、Enum等TypeAdapter可处理的类型 - 暂不支持流式 partial object、多 provider 内置适配、BAML DSL/codegen
Project details
Release history Release notifications | RSS feed
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 structured_llm-0.1.0.tar.gz.
File metadata
- Download URL: structured_llm-0.1.0.tar.gz
- Upload date:
- Size: 59.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7105920df0ac6d05f5e3a9b5097557682f5492309f0067c45ba05d27f0c1400c
|
|
| MD5 |
3ed373a2893a850a59a0e4e6e43c2935
|
|
| BLAKE2b-256 |
dfe886088c7870557250f5e2d65345b3370730de1481c8c2edd39e213fd2d030
|
File details
Details for the file structured_llm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: structured_llm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
204efb35785a8b62c881c348a87530035aa26e214583a373acd8c180953c0cff
|
|
| MD5 |
211bb089cb604b0bf4f04d8f11573173
|
|
| BLAKE2b-256 |
0b6f1f98b9aa5f203adf166f137e8c7df042fa0ce977066f851e64dede74f78a
|