项目介绍
Agent 配套工具 · 2026-11-09 更新 · 本站尚未实测
Instructor 是一个用于从大语言模型获取结构化输出的 Python 库,构建在 Pydantic 之上,提供校验、类型安全与 IDE 支持。README 的定位很明确:「Get reliable JSON from any LLM. Built on Pydantic for validation, type safety, and IDE support.」 它解决的是手写 JSON Schema、解析非结构化响应、处理校验错误、重试失败抽取、适配不同厂商 API 这些重复劳动。
需要特别区分:Instructor 本身是「结构化抽取库」,不是一个 Agent 运行时。README 原文写道 「Use Instructor for fast extraction, reach for PydanticAI when you need agents.」,即需要更完整的 Agent 运行、可观测性或可分享轨迹时,官方建议使用 PydanticAI。因此本项目适合作为工作流中的数据处理环节,而非独立的 Agent 平台。
补充说明
- 仓库元数据显示并非归档状态,但维护活跃度与版本演进请以 GitHub 实际提交为准。
- README 示例中出现的部分模型名称请以厂商当前可用型号为准,本页不据此推断型号可用性。
- 未获取到 MCP 配置与服务端配置资料,本项目不作为 MCP 服务介绍。
核心能力
点击能力名称查看输入、输出和原文依据。
- 模型即 Schema:用 Pydantic
BaseModel定义目标结构,调用时传入response_model,直接得到已校验的强类型对象,无需手动json.loads。 - 自动重试:校验失败时携带错误信息自动重试,可设置
max_retries,示例中配合field_validator对取值做业务约束。 - 流式部分对象:通过
Partial[User]在生成过程中逐步拿到字段填充的中间对象。 - 嵌套结构:支持列表中嵌套子模型等复杂结构,由库自行处理。
- 多厂商统一接口:通过
from_provider采用厂商/模型形式调用;README 示例列出 OpenAI、Anthropic、Google、Ollama(本地)以及 Groq,并支持显式传入 API Key。 - 多语言实现:README 声称简单 API 已提供 Python(原始版本)、TypeScript、Ruby、Go、Elixir、Rust 实现。
实际应用场景
适用场景
- 从自然语言、网页或文档文本中抽取固定字段,例如商品名称、价格、库存状态。
- 把模型输出直接喂给下游程序,避免 JSON 解析失败导致的流程中断。
- 需要边生成边展示的交互界面,利用部分对象提前渲染。
- 在多个模型供应商之间切换而尽量不改动业务代码。
安装与接入
README 给出的安装方式为:
pip install instructor
或使用其他包管理器:
uv add instructor
poetry add instructor
接入上,库本身不内置模型服务,需要你提供可用的模型提供方与凭据。README 示例中既有依赖环境变量的方式(如 OPENAI_API_KEY、ANTHROPIC_API_KEY),也有直接传 api_key 参数的方式。具体某个厂商的完整配置、客户端兼容性请以其官方文档为准;本页不提供未经原文确认的配置参数。文档入口:https://python.useinstructor.com 。
使用条件与限制
- 是否需要部署
- 需自行准备运行环境,具体要求见正文
- 是否需要注册
- 账号和认证要求见正文;未明确说明的项目仍待核验
- 是否需要 API Key
- 待核验
- 费用与额度
- 费用边界见正文,项目许可不代表模型或托管服务免费
- 所需权限
- 待核验
- 许可证与服务条款
- 待核验
权限费用与限制
- 费用:Instructor 是代码库,模型调用费用由你选择的服务商按调用量计收,本页无法给出价格,需向对应服务商核验。
- 凭据:需自备各厂商 API Key;使用本地 Ollama 时按本地部署情况而定。
- 数据流向:调用云端模型时,待抽取文本会发送至对应厂商,请自行评估数据合规要求;使用本地模型可减少外发。
- 能力边界:结构化输出质量依赖所选模型能力与 Schema 复杂度,本项目不保证抽取准确率;重试会增加调用次数与成本。
国内使用情况
网友核验 用户经验分享
与站长实测独立,不代表官方认证或本站已实测。
最近核验:暂无有效核验。参与人数按当前项目有效身份去重;不同核验项分别统计,不合并成用户总结果。仅统计审核通过且未过期的记录。
分项统计、有效期与样本量
| 核验项目 | 有效人数 | 成功 / 部分 / 失败 / 无法判断 | 成功率 | 最近核验 / 有效期 |
|---|---|---|---|---|
| 官方网站访问 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 7 天 |
| GitHub 仓库访问 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 7 天 |
| 安装与运行 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 官方服务国内连接 / MCP 连接 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 国内网络情况 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 7 天 |
| 国内模型兼容性 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 自定义 API | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 海外服务依赖 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 海外账号要求 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 中文界面 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 客户端兼容性 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
| 账号及费用信息 | 0 | 0 / 0 / 0 / 0 | 样本不足 | 暂无 / 30 天 |
成功率 = 成功 ÷(成功 + 部分成功 + 失败);无法判断不计入分母。至少 5 份可判断结果才显示成功率。账号、费用、海外依赖与语言等事实项,成功表示核查结论与资料吻合,具体观察另行统计;不表示免费或没有海外依赖。不同网络、客户端和版本的结果不能直接等同。
最近有效核验
暂无审核通过且未过期的网友核验。欢迎提交真实使用记录。
查看全部有效核验 →国内实测状态:国内可用性待核验。协议兼容和官方资料说明不等于国内网络可用。
- 官方网站访问
- 尚未实测 待核验
- GitHub 仓库访问
- 尚未实测 待核验
- 官方服务国内连接
- 尚未实测 待核验
- 国内网络情况
- 尚未实测 待核验
- 国内模型兼容性
- 尚未实测 待核验
- 自定义 API
- 尚未实测 待核验
- 海外服务依赖
- 尚未实测 待核验
- 海外账号要求
- 尚未实测 待核验
- 中文界面
- 尚未实测 待核验
- 国内最后核验时间
- 尚未实测
许可与国内使用
仓库 LICENSE 文件为 MIT License(Copyright (c) 2023 Jason Liu),可免费使用、修改与分发,需保留版权与许可声明,软件按「AS IS」提供、不附担保。该许可仅覆盖本仓库代码,不覆盖第三方模型服务、云端 API 及其条款。
关于中国大陆网络环境下的可用性、各厂商 API 是否可直连、是否支持国内模型厂商,README 未作说明,均待核验。域名与文档站点不代表服务部署地域。
项目资料与来源
字段依据与待核验事项
access:来源确认 · pip install instructor
project_type:来源确认 · Get reliable JSON from any LLM. Built on Pydantic for validation, type safety, and IDE support.
原始 README(折叠技术参考)
# Instructor: Structured Outputs for LLMs
<p align="center">
<b>English</b> · <a href="https://github.com/567-labs/instructor/blob/main/README.zh.md">简体中文</a>
</p>
Get reliable JSON from any LLM. Built on Pydantic for validation, type safety, and IDE support.
```python
import instructor
from pydantic import BaseModel
# Define what you want
class User(BaseModel):
name: str
age: int
# Extract it from natural language
client = instructor.from_provider("openai/gpt-4o-mini")
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "John is 25 years old"}],
)
print(user) # User(name='John', age=25)
```
**That's it.** No JSON parsing, no error handling, no retries. Just define a model and get structured data.
[](https://pypi.org/project/instructor/)
[](https://pypi.org/project/instructor/)
[](https://github.com/567-labs/instructor)
[](https://discord.gg/bD9YE9JArw)
[](https://twitter.com/jxnlco)
> **Use Instructor for fast extraction, reach for PydanticAI when you need agents.** Instructor keeps schema-first flows simple and cheap. If your app needs richer agent runs, built-in observability, or shareable traces, try [PydanticAI](https://ai.pydantic.dev/). PydanticAI is the official agent runtime from the Pydantic team, adding typed tools, replayable datasets, evals, and production dashboards while using the same Pydantic models. Dive into the [PydanticAI docs](https://ai.pydantic.dev/) to see how it extends Instructor-style workflows.
## Why use Instructor?
Getting structured data from LLMs is hard. You need to:
1. Write complex JSON schemas
2. Handle validation errors
3. Retry failed extractions
4. Parse unstructured responses
5. Deal with different provider APIs
**Instructor handles all of this with one simple interface:**
<table>
<tr>
<td><b>Without Instructor</b></td>
<td><b>With Instructor</b></td>
</tr>
<tr>
<td>
```python
response = openai.chat.completions.create(
model="gpt-5.4-mini",
messages=[{"role": "user", "content": "..."}],
tools=[
{
"type": "function",
"function": {
"name": "extract_user",
"parameters": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
},
},
}
],
)
# Parse response
tool_call = response.choices[0].message.tool_calls[0]
user_data = json.loads(tool_call.function.arguments)
# Validate manually
if "name" not in user_data:
# Handle error...
pass
```
</td>
<td>
```python
client = instructor.from_provider("openai/gpt-5.4-mini")
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
)
# That's it! user is validated and typed
```
</td>
</tr>
</table>
## Install in seconds
```bash
pip install instructor
```
Or with your package manager:
```bash
uv add instructor
poetry add instructor
```
## Works with every major provider
Use the same code with any LLM provider:
```python
import os
# OpenAI
client = instructor.from_provider("openai/gpt-4o")
# Anthropic
client = instructor.from_provider("anthropic/claude-sonnet-5")
# Google
client = instructor.from_provider("google/gemini-3.8-flash")
# Ollama (local)
client = instructor.from_provider("ollama/llama3.2")
# With API keys directly (no environment variables needed)
client = instructor.from_provider("openai/gpt-4o", api_key=os.environ["OPENAI_API_KEY"])
client = instructor.from_provider(
"anthropic/claude-sonnet-5", api_key=os.environ["ANTHROPIC_API_KEY"]
)
client = instructor.from_provider("groq/llama-3.1-8b-instant", api_key="gsk_...")
# All use the same API!
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
)
```
## Production-ready features
### Automatic retries
Failed validations are automatically retried with the error message:
```python
from pydantic import BaseModel, field_validator
class User(BaseModel):
name: str
age: int
@field_validator('age')
def validate_age(cls, v):
if v < 0:
raise ValueError('Age must be positive')
return v
# Instructor automatically retries when validation fails
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
max_retries=3,
)
```
### Streaming support
Stream partial objects as they're generated:
```python
from instructor import Partial
for partial_user in client.chat.completions.create(
response_model=Partial[User],
messages=[{"role": "user", "content": "..."}],
stream=True,
):
print(partial_user)
# User(name=None, age=None)
# User(name="John", age=None)
# User(name="John", age=25)
```
### Nested objects
Extract complex, nested data structures:
```python
from typing import List
class Address(BaseModel):
street: str
city: str
country: str
class User(BaseModel):
name: str
age: int
addresses: List[Address]
# Instructor handles nested objects automatically
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
)
```
## Used in production by
Trusted by over 100,000 developers and companies building AI applications:
- **3M+ monthly downloads**
- **10K+ GitHub stars**
- **1000+ community contributors**
Companies using Instructor include teams at OpenAI, Google, Microsoft, AWS, and many YC startups.
## Get started
### Basic extraction
Extract structured data from any text:
```python
from pydantic import BaseModel
import instructor
client = instructor.from_provider("openai/gpt-4o-mini")
class Product(BaseModel):
name: str
price: float
in_stock: bool
product = client.chat.completions.create(
response_model=Product,
messages=[{"role": "user", "content": "iPhone 15 Pro, $999, available now"}],
)
print(product)
# Product(name='iPhone 15 Pro', price=999.0, in_stock=True)
```
### Multiple languages
Instructor's simple API is available in many languages:
- [Python](https://python.useinstructor.com) - The original
- [TypeScript](https://js.useinstructor.com) - Full TypeScript support
- [Ruby](https://ruby.useinstructor.com) - Ruby implementation
- [Go](https://go.useinstructor.com) - Go implementation
- [Elixir](https://hex.pm/packages/instructor) - Elixir implementation
- [Rust](https://rust.useinstructor.com) - Rust implementation
### Learn more
- [Documentation](https://python.useinstructor.com) - Comprehensive guides
- [Examples](https://python.useinstructor.com/examples/) - Copy-paste recipes
- [Blog](https://python.useinstructor.com/blog/) - Tutorials and best practices
- [Discord](https://discord.gg/bD9YE9JArw) - Get help from the community
## Why use Instructor over alternatives?
**vs Raw JSON mode**: Instructor provides automatic validation, retries, streaming, and nested object support. No manual schema writing.
**vs LangChain/LlamaIndex**: Instructor is focused on one thing - structured extraction. It's lighter, faster, and easier to debug.
**vs Custom solutions**: Battle-tested by thousands of developers. Handles edge cases you haven't thought of yet.
## Contributing
We welcome contributions! Check out our [good first issues](https://github.com/567-labs/instructor/labels/good%20first%20issue) to get started.
## License
MIT License - see [LICENSE](https://github.com/567-labs/instructor/blob/main/LICENSE) for details.
---
<p align="center">
Built by the Instructor community. Special thanks to <a href="https://twitter.com/jxnlco">Jason Liu</a> and all <a href="https://github.com/567-labs/instructor/graphs/contributors">contributors</a>.
</p>历史前置条件说明(补充参考)
Python 环境;通过 pip/uv/poetry 安装;需自备模型提供方 API Key(如 OPENAI_API_KEY、ANTHROPIC_API_KEY)或本地 Ollama 服务;模型调用费用由服务商按量计收,价格待核验;国内网络可用性与国内模型支持待核验。
项目来源与清单版权许可
https://github.com/567-labs/instructor
https://python.useinstructor.com/
整理来源:https://github.com/slavakurilyak/awesome-ai-agents
清单版权与许可:
MIT License
Copyright (c) 2024 Slava Kurilyak
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.