सरल भाषा में
जब आप LLM को कॉल करते हैं, तो वह आपको एक string लौटाता है। वह string JSON जैसा दिख सकता है, या एक पैराग्राफ हो सकता है, या फिर आपके निर्देशों को पूरी तरह अनदेखा करके ऐसा कुछ लौटा सकता है जिसे आप parse नहीं कर सकते। एक छोटे डेमो के लिए यह ठीक है। लेकिन production code के लिए, जो customer refund रूट करता है या database write ट्रिगर करता है, यह जोखिम भरा है।

PydanticAI एक Python agent framework है, जिसे उसी टीम ने बनाया है जो Pydantic के पीछे है, वह validation library जिसे OpenAI, Anthropic और Google तीनों अपने अंदर इस्तेमाल करते हैं। इसका मूल विचार है: AI agents को आपके बाकी Python code जैसी ही type guarantees मिलनी चाहिए। आप घोषित करते हैं कि output किस shape का होना चाहिए (एक Pydantic model), agent कौन से tools कॉल कर सकता है (full type hints वाले decorated functions), और उसे कौन सी बाहरी dependencies चाहिए (typed context के ज़रिए injected)। बाकी काम framework संभालता है: schema generation, JSON validation, खराब output पर retry और observability।
सबसे साफ़ तुलना है agents के लिए FastAPI। FastAPI ने Python types से HTTP request/response का shape घोषित करने और framework को validation, docs और error handling अपने आप बनाने देने का चलन लोकप्रिय किया। PydanticAI वही दर्शन LLM-powered agents पर लागू करता है: सब कुछ types से घोषित करें, और framework उन्हें runtime पर लागू करे।
यह क्यों मायने रखता है
ज़्यादातर agent frameworks LLM output को एक string मानते हैं, जिसे आगे कहीं parse किया जाता है। इससे ऐसी bugs पैदा होती हैं जो सिर्फ runtime पर दिखती हैं, अक्सर production में: गलत format का JSON, छूटे हुए required fields, गलत data types, और ऐसा injected content जो आगे के logic को तोड़ दे। PydanticAI call से पहले LLM का contract स्पष्ट करके और call के तुरंत बाद उसे validate करके इस पूरी श्रेणी को ही खत्म कर देता है।
production teams के लिए तीन ठोस फ़ायदे
- Bugs runtime से write-time पर आ जाते हैं। जब आप code लिखते हैं, तभी आपका IDE type mismatches को पकड़ लेता है, न कि तब जब कोई customer टूटे हुए रास्ते पर पहुँचता है।
- Dependencies को अलग से test करें। क्योंकि tools global state के बजाय typed, injected dependencies प्राप्त करते हैं, आप tests में असली database client को mock से एक ही line बदलकर स्वैप कर सकते हैं।
- Model बदलना आसान है। PydanticAI OpenAI, Anthropic, Google, xAI, Bedrock, Cohere, Mistral, Groq, Ollama और अन्य को सपोर्ट करता है। Provider बदलने के लिए सिर्फ एक string बदलनी होती है, कोई code rewrite नहीं।
यह मायने रखता है क्योंकि AI agents जो tools कॉल करते हैं (databases देखते हैं, files लिखते हैं, APIs कॉल करते हैं), वे ऐसे काम कर रहे होते हैं जिन्हें वापस लेना कठिन या असंभव होता है। उन tools में बिना type और बिना validation वाला data जाना ठीक वही तरह की चुपचाप होने वाली विफलता है जो असली दुनिया की घटनाओं का कारण बनती है। PydanticAI इसी threat model को ध्यान में रखकर बनाया गया है।
यह कैसे काम करता है
एक PydanticAI application के चार मुख्य building blocks हैं: Agent, टूल, Dependencies और Structured Output। इनमें से हर एक सीधे किसी Python type या decorator से मेल खाता है।
Agent परिभाषित करना
एक Agent को दो generics से typed किया जाता है: dependency type और output type। दोनों सामान्य Python types हैं: dataclasses, Pydantic models, या primitives।
from pydantic import BaseModel
from pydantic_ai import Agent, RunContext
class OrderDeps:
db: object # your real DB client here
class OrderSummary(BaseModel):
order_id: str
status: str
total_usd: float
agent: Agent[OrderDeps, OrderSummary] = Agent(
'anthropic:claude-sonnet-4-6',
deps_type=OrderDeps,
output_type=OrderSummary,
system_prompt='You are an order support assistant.',
)Typed tools जोड़ना
Tools वे सामान्य Python functions हैं जिन्हें @agent.tool से decorate किया जाता है। Function का docstring वह description बन जाता है जो LLM देखता है; parameter types tool arguments के लिए JSON schema बन जाते हैं। PydanticAI आपका code चलने से पहले ही arguments को validate कर देता है।
@agent.tool
async def lookup_order(ctx: RunContext[OrderDeps], order_id: str) -> dict:
"""Look up an order by its ID and return status and total."""
return await ctx.deps.db.get_order(order_id)Structured output और अपने आप retry
जब LLM अपना अंतिम response लौटाता है, तो PydanticAI उसे output_type के विरुद्ध Pydantic validation से गुज़ारता है। अगर validation विफल होता है (कोई field छूटा है, type गलत है, या कोई value constraint का उल्लंघन करती है), तो error message follow-up message के रूप में LLM को वापस भेजा जाता है, ताकि वह खुद को सुधार सके। आप कितने retries की अनुमति देंगे, यह configure कर सकते हैं।
import asyncio
async def main():
deps = OrderDeps(db=my_db_client)
result = await agent.run('What is the status of order ORD-9821?', deps=deps)
# result.output is a fully validated OrderSummary instance
print(result.output.status) # 'shipped'
print(result.output.total_usd) # 142.50 (a real float, not a string)
asyncio.run(main())मुख्य design choices
Global state के बजाय dependency injection
कई frameworks database clients या API keys जैसी services जोड़ने के लिए global singletons या environment variables पर निर्भर रहते हैं। PydanticAI इसके बजाय formal dependency injection इस्तेमाल करता है: आप agent के लिए deps_type घोषित करते हैं, call के समय उसका एक instance देते हैं, और हर tool RunContext के ज़रिए उस तक पहुँच सकता है। इससे agents unit-testable बनते हैं, यानी globals को patch किए बिना असली client को test double से बदला जा सकता है।
डिज़ाइन से model-agnostic
Agent का model 'openai:gpt-5.5' या 'anthropic:claude-sonnet-4-6' जैसा string identifier है। Provider बदलना एक-line का बदलाव है, क्योंकि model-specific adapter logic आपके code में नहीं, PydanticAI के अंदर रहता है। सपोर्टेड providers में OpenAI, Anthropic, Google Gemini, xAI Grok, Mistral, Cohere, Groq, Amazon Bedrock, Hugging Face, Ollama और OpenRouter शामिल हैं।
Pydantic Logfire के साथ observability
PydanticAI Pydantic Logfire के साथ native रूप से जुड़ता है, जो एक OpenTelemetry-based observability platform है। हर agent run, tool call और model exchange अपने आप trace होता है: आप देखते हैं कि model ने क्या भेजा, क्या लौटाया, कौन से tools चले, और हर step में कितना समय लगा। यह built-in है, बाद में जोड़ा हुआ नहीं।
Human-in-the-loop और durable execution
Tools को चलने से पहले स्पष्ट human approval चाहिए, ऐसा चिह्नित किया जा सकता है। Durable execution support के साथ (framework failures के पार agent runs को checkpoint और resume कर सकता है), PydanticAI उन लंबे, उच्च-जोखिम वाले workflows के लिए बनाया गया है जहाँ चुपचाप होने वाली विफलता या खोई हुई प्रगति बर्दाश्त नहीं की जा सकती।
- Output एक raw string है
- JSON हाथ से parse करें, exceptions संभालें
- Tool args हाथ से validate होते हैं (या बिल्कुल नहीं)
- Model स्वैप = adapter code दोबारा लिखें
- Test के लिए globals को patch करना पड़ता है
- Bugs production में runtime पर सामने आते हैं
- Output एक typed Pydantic model है
- Validation अपने आप चलता है, विफल होने पर retry होता है
- Tool args type hints से validate होते हैं
- Model स्वैप = एक string बदलें
- Injected mock deps से test करें
- Type errors IDE द्वारा write-time पर पकड़ी जाती हैं
और गहराई से
बुनियादी बातों में सहज हो जाने के बाद, PydanticAI की कई उन्नत क्षमताएँ हैं जिन्हें देखना उपयोगी है।
Streaming structured output
PydanticAI model का output token-by-token stream कर सकता है और साथ-साथ partial Pydantic validation भी चला सकता है। इससे आपका UI type safety खोए बिना progressive results दिखा सकता है, और पहला valid partial object जितनी जल्दी हो सके पहुँचता है।
Graph-based workflows
जटिल multi-step pipelines के लिए PydanticAI में एक graph API है, जहाँ nodes typed Python functions होते हैं जो type-checked edges से जुड़े होते हैं। जब आपके workflow में branching logic, loops या parallel paths हों जिन्हें एक अकेला agent call नहीं समेट सकता, तब यही सही tool है। इसे LangGraph जैसी किसी चीज़ का statically-typed विकल्प समझें।
MCP और multi-agent protocols
PydanticAI Model Context Protocol (MCP) और Agent2Agent (A2A) protocol को सपोर्ट करता है, जिससे agents अपने tools MCP-compatible clients को expose कर सकते हैं और typed, interoperable तरीके से दूसरे agents को काम सौंप सकते हैं।
PydanticAI कब चुनें
- आप अपने stack में पहले से Pydantic data validation के लिए इस्तेमाल करते हैं, तो PydanticAI स्वाभाविक रूप से फिट हो जाता है।
- आपको production-grade reliability चाहिए: validated outputs, typed tools और formal dependency injection, ये सब विफलता की संभावनाएँ कम करते हैं।
- आप testable agents चाहते हैं: dependency injection के कारण असली API calls के बिना agent logic की unit testing सीधी हो जाती है।
- आपको multi-model लचीलापन चाहिए: model-agnostic डिज़ाइन vendor lock-in को रोकता है।
- आप structured-output pipelines बना रहे हैं: अगर आपके agent का काम डेटा को किसी तय schema में निकालना या बदलना है, तो PydanticAI इसी के लिए बना है।
अक्सर पूछे जाने वाले प्रश्न
क्या PydanticAI, Pydantic library जैसा ही है?
नहीं। Pydantic एक स्वतंत्र data-validation library है (JSON को Python objects में parse करने के लिए व्यापक रूप से इस्तेमाल होती है)। PydanticAI एक अलग agent framework है, जो उसी टीम ने Pydantic के ऊपर बनाया है। आप PydanticAI के बिना Pydantic इस्तेमाल कर सकते हैं, लेकिन PydanticAI schema generation और output validation के लिए Pydantic का भारी इस्तेमाल करता है।
क्या PydanticAI सिर्फ OpenAI के साथ काम करता है?
नहीं, यह स्पष्ट रूप से model-agnostic है। सपोर्टेड providers में OpenAI, Anthropic, Google Gemini, xAI Grok, Mistral, Cohere, Groq, Amazon Bedrock, Hugging Face, Ollama (local models के लिए) और OpenRouter शामिल हैं। Provider बदलना एक string बदलने जितना आसान है।
जब LLM ऐसा output लौटाता है जो Pydantic validation में विफल हो जाता है, तब क्या होता है?
PydanticAI validation error को अपने आप follow-up message के रूप में model को वापस भेजता है, और उससे output सुधारने को कहता है। आप agent पर retries सीमा configure करते हैं। अगर उतने प्रयासों के बाद भी model विफल रहता है, तो एक typed exception उठाया जाता है ताकि आप उसे स्पष्ट रूप से संभाल सकें।
PydanticAI, LangChain से कैसे अलग है?
LangChain कहीं व्यापक framework है (chains, memory, retrieval, कई integrations), जो धीरे-धीरे विकसित हुआ और ऐतिहासिक रूप से string-based data flow पर निर्भर रहा। PydanticAI संकरा और अधिक opinionated है: यह validated I/O और formal dependency injection वाले typed agents पर केंद्रित है। टीमें अक्सर बताती हैं कि production agent use cases में PydanticAI को कम boilerplate चाहिए, हालाँकि दोनों frameworks की अपनी-अपनी खूबियाँ हैं।
क्या PydanticAI streaming responses के लिए इस्तेमाल किया जा सकता है?
हाँ। PydanticAI streaming structured output को सपोर्ट करता है: model token generate करते समय partial validated objects भेजता है, पूरे response का इंतज़ार नहीं करता। यह chatbot UIs के लिए उपयोगी है, जहाँ आप type safety छोड़े बिना progressive display चाहते हैं।
क्या PydanticAI इस्तेमाल करने से पहले Pydantic जानना ज़रूरी है?
Pydantic की बुनियादी जानकारी बहुत मदद करती है, क्योंकि आप output schemas को Pydantic BaseModel subclasses के रूप में परिभाषित करेंगे। अगर आप पहले से FastAPI इस्तेमाल कर चुके हैं (जो Pydantic भी इस्तेमाल करता है), तो मानसिक मॉडल सीधे लागू होता है। अगर नहीं, तो Pydantic के अपने docs छोटे और सरल हैं।
