pydantic-ai
ما الذي تفعله
PydanticAI مجموعة أدوات لبناء وكلاء الذكاء الاصطناعي بلغة Python. تضمن أن تكون ردود الذكاء الاصطناعي منظمة وصحيحة، مما يسهل بناء تطبيقات ذكاء اصطناعي متينة وقابلة للاختبار.
متى تستخدمها
- ✓عندما تبني وكلاء ذكاء اصطناعي بلغة Python يحتاجون إلى التفاعل مع أدوات أخرى.
- ✓عندما تريد أن يعيد الذكاء الاصطناعي بيانات منظمة ومتحققا منها، لا مجرد نص عشوائي.
- ✓لاختبار منطق وكيل الذكاء الاصطناعي دون استدعاء الذكاء الاصطناعي الحقيقي في كل مرة.
رابط المهارة
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/pydantic-ai/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“كيف أنشئ وكيل ذكاء اصطناعي بلغة Python يعيد ملخصا نصيا بصيغة محددة؟”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
تحدد بنية الرد المتوقعة باستخدام نماذج Pydantic. تضمن PydanticAI أن يلتزم الذكاء الاصطناعي بهذه البنية ويتحقق من المخرجات. كما تتيح ربط الذكاء الاصطناعي بأدوات أخرى واختبار كل شيء بسهولة.
المحتوى التقني (ملف SKILL.md الأصلي)
النص الأصلي من المؤلف، مباشرة من GitHub.
PydanticAI — Typed AI Agents in Python
Overview
PydanticAI is a Python agent framework from the Pydantic team that brings the same type-safety and validation guarantees as Pydantic to LLM-based applications. It supports structured outputs (validated with Pydantic models), dependency injection for testability, streamed responses, multi-turn conversations, and tool use — across OpenAI, Anthropic, Google Gemini, Groq, Mistral, and Ollama. Use this skill when building production AI agents, chatbots, or LLM pipelines where correctness and testability matter.
When to Use This Skill
- Use when building Python AI agents that call tools and return structured data
- Use when you need validated, typed LLM outputs (not raw strings)
- Use when you want to write unit tests for agent logic without hitting a real LLM
- Use when switching between LLM providers without rewriting agent code
- Use when the user asks about
Agent,@agent.tool,RunContext,ModelRetry, orresult_type
How It Works
Step 1: Installation
pip install pydantic-ai
# Install extras for specific providers
pip install 'pydantic-ai[openai]' # OpenAI / Azure OpenAI
pip install 'pydantic-ai[anthropic]' # Anthropic Claude
pip install 'pydantic-ai[gemini]' # Google Gemini
pip install 'pydantic-ai[groq]' # Groq
pip install 'pydantic-ai[vertexai]' # Google Vertex AI
Step 2: A Minimal Agent
from pydantic_ai import Agent
# Simple agent — returns a plain string
agent = Agent(
'anthropic:claude-sonnet-4-6',
system_prompt='You are a helpful assistant. Be concise.',
)
result = agent.run_sync('What is the capital of Japan?')
print(result.data) # "Tokyo"
print(result.usage()) # Usage(requests=1, request_tokens=..., response_tokens=...)
Step 3: Structured Output with Pydantic Models
from pydantic import BaseModel
from pydantic_ai import Agent
class MovieReview(BaseModel):
title: str
year: int
rating: float # 0.0 to 10.0
summary: str
recommended: bool
agent = Agent(
'openai:gpt-4o',
result_type=MovieReview,
system_prompt='You are a film critic. Return structured reviews.',
)
result = agent.run_sync('Review Inception (2010)')
review = result.data # Fully typed MovieReview instance
print(f"{review.title} ({review.year}): {review.rating}/10")
print(f"Recommended: {review.recommended}")
Step 4: Tool Use
Register tools with @agent.tool — the LLM can call them during a run:
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel
import httpx
class WeatherReport(BaseModel):
city: str
temperature_c: float
condition: str
weather_agent = Agent(
'anthropic:claude-sonnet-4-6',
result_type=WeatherReport,
system_prompt='Get current weather for the requested city.',
)
@weather_agent.tool
async def get_temperature(ctx: RunContext, city: str) -> dict:
"""Fetch the current temperature for a city from the weather API."""
async with httpx.AsyncClient() as client:
r = await client.get(f'https://wttr.in/{city}?format=j1')
data = r.json()
return {
'temp_c': float(data['current_condition'][0]['temp_C']),
'description': data['current_condition'][0]['weatherDesc'][0]['value'],
}
import asyncio
result = asyncio.run(weather_agent.run('What is the weather in Tokyo?'))
print(result.data)
Step 5: Dependency Injection
Inject services (database, HTTP clients, config) into agents for testability:
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel
@dataclass
class Deps:
db: Database
user_id: str
class SupportResponse(BaseModel):
message: str
escalate: bool
support_agent = Agent(
'openai:gpt-4o-mini',
deps_type=Deps,
result_type=SupportResponse,
system_prompt='You are a support agent. Use the tools to help customers.',
)
@support_agent.tool
async def get_order_history(ctx: RunContext[Deps]) -> list[dict]:
"""Fetch recent orders for the current user."""
return await ctx.deps.db.get_orders(ctx.deps.user_id, limit=5)
@support_agent.tool
async def create_refund(ctx: RunContext[Deps], order_id: str, reason: str) -> dict:
"""Initiate a refund for a specific order."""
return await ctx.deps.db.create_refund(order_id, reason, ctx.deps.user_id)
# Usage
async def handle_support(user_id: str, message: str):
…
الأسئلة الشائعة
ما هي « نماذج Pydantic »؟
هي أشبه بنماذج تحددها في Python لوصف بنية البيانات المتوقعة. مثلا، نموذج لبيانات فيلم قد يحتوي على حقول مثل « العنوان » و« السنة » و« التقييم ».
ماذا يعني « type-safe »؟
يعني أن الشيفرة تتحقق من أنواع البيانات (نص، رقم، قيمة منطقية) لمنع الأخطاء. وتضيف PydanticAI هذا الأمان إلى تفاعلات الذكاء الاصطناعي.
كيف أختبر وكيل الذكاء الاصطناعي دون إنفاق مال أو وقت؟
تتيح PydanticAI استخدام « نماذج اختبار » تحاكي ردود الذكاء الاصطناعي، فتختبر منطق وكيلك دون إجراء استدعاءات فعلية لخدمات ذكاء اصطناعي مكلفة.
التفاصيل
الوصف الأصلي من المؤلف
Build production-ready AI agents with PydanticAI — type-safe tool use, structured outputs, dependency injection, and multi-model support.
الشعبية
- نجوم GitHub
- 48k
- الترتيب
- #5541
- الترخيص
- MIT
- آخر تحديث
- 15/10/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
00-andruia-consultant
48kيساعد وكيلك على بدء مشاريع الذكاء الاصطناعي أو تحسينها. يحلل الكود ويقترح الأدوات ويُعد خطط عمل بالإسبانية.
007
48kيتيح لوكيلك إجراء تدقيقات أمنية ونمذجة التهديدات. مفيد لحماية المشاريع من الهجمات والثغرات الأمنية.
10-andruia-skill-smith
48kيساعد وكيلك على إنشاء أدوات الذكاء الاصطناعي أو تحسينها. يحدد طريقة عملها، ويكتب توثيقها، ويتحقق من بنيتها.
20-andruia-niche-intelligence
48kيتيح لوكيلك إعداد ملخص عن صناعة محددة. مفيد لفهم السياق والقواعد وأنماط الاستخدام والمخاطر لمشروع ما، باستخدام مصادر موثوقة.
مهارات مشابهة
api-connector-builder
280kيبني وكيلك اتصالات API جديدة على النمط الموجود، ويفيد عندما تحتاج إلى تكامل إضافي دون ابتكار طريقة جديدة للعمل.
api-design
280kيساعد وكيلك على تصميم واجهات REST API وفق أفضل الممارسات، ويوضح كيف تتواصل وكيف تُعرض الأخطاء وكيف تُنظَّم البيانات.
backend-patterns
280kيساعد وكيلك على أنماط بنية الخادم وتحسين قاعدة البيانات، ويفيد في بناء الشيفرة التي تعمل على الخادم أو مراجعتها.