AI Agents for E-commerce with Pydantic AI
How to automate customer support and order management in e-commerce applications
December 20, 2024
E-commerce businesses face incredibly complex challenges to provide an exceptional customer service: customers expect instant, accurate responses 24/7, detailed product information at their fingertips, and efficient order management at scale. Meeting these expectations has traditionally forced businesses to choose between aggressive hiring (expensive) or service compromises (risky).
But what if there was a better way?
The rise of Large Language Models (LLMs) has opened new possibilities for automating complex customer interactions. Consider these real-world scenarios:
- A customer asking detailed questions about product materials and care instructions at 3 AM.
- Someone needing to update their shipping address on a pending order.
- A request for return processing with specific item conditions.
Previously, each of these required human intervention. Now, they can be handled automatically, accurately, and instantly by AI agents. Modern AI agents can understand context, process natural language and execute complex actions. However, there's the catch: given the probabilistic nature of LLMs, building production-ready AI agents that you can actually trust has been challenging.
#Pydantic AI: A Game Changer for AI Agents
Pydantic is my favourite Python library and I use it in practically every Python project I build to handle data parsing and validation, from FastAPI backends to data processing pipelines. And if you are a Python developer, you probably have used it too.
Pydantic has already proven to be essential for LLM applications that need structured outputs or function calling capabilities. It's no coincidence that libraries like Instructor, LangChain, and LlamaIndex, as well as OpenAI's and Anthropic's own SDKs, use it internally. Or to quote Jason Liu's influential talk: "Pydantic is all you need."
Two weeks ago, Pydantic creator Samuel Colvin and his team released PydanticAI, a game-changing library for building AI agents in Python.
What sets PydanticAI apart? Its simplicity and clean architecture. While other frameworks pile on abstractions, PydanticAI provides a thin, type-safe layer for building AI agents. If you've used FastAPI, you'll feel right at home — the library embraces Python's native features and builds on familiar tools, making it both powerful and approachable.
Out of the box, PydanticAI delivers a set of features that are essential for production generative AI applications: type safety, definition and validation of structured outputs, streaming support, a dependency injection system, and model flexibility (supporting different model providers like OpenAI, Anthropic, Gemini, Ollama, Groq and Mistral). Make sure to check the PydanticAI documentation for more details.
In the following two sections, we'll go from theory to practice and learn how to use the PydanticAI library to build two AI agents for a fashion e-commerce store:
- A support agent that assists customers by answering their questions using Retrieval-Augmented Generation (RAG).
- An order management agent that processes complex operations like updates, returns and cancellations.
You can find all the code in detail in the following GitHub repository.
While these examples are slightly simplified to serve as an introduction to agent building, they mirror the patterns and approaches you'd use in production systems. You will see how PydanticAI helps you build AI agents that work reliably and solve real business problems. Ready to dive in?
#Customer Support RAG Agent
Let's start with a scenario every e-commerce business knows well: customers asking detailed questions about products, shipping policies and store information at all hours. Even if the questions aren't particularly complex, they require accurate, consistent answers drawn from your product catalog and company policies.
Retrieval-Augmented Generation (RAG) is a powerful technique to solve this problem. It combines the capabilities of a Large Language Model (LLM) with your own knowledge base to provide accurate responses to user questions.
The core idea is simple:
- We give our support agent access to a knowledge base containing all the relevant information about products, prices and company policies.
- When a customer asks a question, the agent will search the knowledge base and deliver an accurate response based on this information, in a natural and conversational manner.
Let's see how to implement it using PydanticAI. We'll build a customer support agent for Maison Noir, a luxury fashion e-commerce store that we will use as a fictional example for this blog post.
The core of any PydanticAI application is the Agent
class, which can be configured with a specific model, dependencies and a system prompt:
PydanticAI uses a clean dependency injection system to provide data and services to your agents. In our case, we're providing a custom KnowledgeBase
class as a dependency, so that we are able to search it from the agent's tools.
In RAG applications, you would normally preprocess, chunk, embed and store your documents in a vector database, and ideally combine vector search with full-text search for maximum accuracy. We have explored these ideas in previous blog posts where we build RAG from scratch, and use Redis or PostgreSQL as vector databases for RAG.
For this example, we're using a extremely simplified knowledge base that just returns our store's information:
The beauty of this design is that in a real application, you would only need to change the implementation of the search
method to use your preferred database and search strategy. The rest of the agent code remains exactly the same, completely independent of how the knowledge retrieval is implemented.
Now we need to give our agent the ability to query the knowledge base. PydanticAI makes this straightforward with its tool system. Tools are simply functions that the agent can call when needed. We can easily define a tool using the @agent.tool
decorator:
This is where the use of Pydantic truly shines. When we create a tool, PydanticAI automatically extracts parameters from the function signature, extracts the function description from the docstring and creates a tool schema that the LLM can understand. All of this happens automatically behind the scenes, making our code simpler and cleaner.
Notice also how the tool's first argument gives access to the agent's context, which includes the agent's dependencies (in our case, the knowledge base that we want to search).
Now our agent is ready to handle customer queries. We can simply run it like this, remembering to pass the knowledge base as a dependency:
You can also explore the new messages generated during the agent run using result.new_messages()
and you will get a result like this:
As you can see above, the agent internally calls the search_knowledge_base
tool to query the knowledge base and then builds the response using the retrieved information.
While querying the agent with single questions is useful, most e-commerce applications need to support full conversations with customers. PydanticAI makes this straightforward by allowing us to maintain conversation history. We just need to run the agent in a loop and make sure to update the message history with the new messages generated in every agent call:
For the best possible user experience, PydanticAI supports response streaming. Instead of waiting for complete responses, customers see them being generated in real-time, creating a more natural and engaging interaction. Implementing streaming is straightforward:
⚠️The final result message will NOT be added to result messages if you use stream_text(delta=True)
, so we need to add it manually.
You can try the agent yourself using the code in the GitHub repository. Simply clone the repository, install the dependencies and run:
#Orders Management Agent
Now that we've introduced the main concepts and components of PydanticAI through our RAG agent, let's tackle something more ambitious: order management. This agent will handle a variety of operations including:
- Checking order status and details.
- Updating shipping addresses.
- Processing order cancellations.
- Handling return requests.
- Escalating complex cases to human support.
Here is where things get interesting. We are moving beyond simple question answering into actually executing actions that affect real orders and customers. Every operation requires careful validation, state management and communication. Let's see how PydanticAI helps us build a robust system that handles these complexities elegantly.
First of all, we need to create a set of enums and Pydantic models for our order management system:
Notice how as well as defining data structures, we are encoding business rules directly into our Pydantic models that will help during validation. The can_modify
and can_return
properties ensure that orders are only modified in appropriate states and that returns are only allowed within 30 days of delivery.
Let's now create the orders agent:
We are now using a dataclass
for dependencies instead of passing the database directly. While not strictly necessary here, this pattern is more convenient when you need to inject multiple dependencies into your agent.
The orders agent will run in a simple loop that maintains conversation history and handles streaming responses, very similar to the run_agent_streaming
function we saw previously, so there is no need to repeat it here too.
You can try it yourself by cloning the GitHub repository, installing the dependencies and running:
The most interesting part of the orders agent is the toolkit that it has available to perform all the necessary functionalities. Each tool represents a specific order management operation, complete with its own validation and business logic. Let's take a look at each one in detail.
#Order Status & Details Retrieval
Our first tool handles order status and details inquiries:
The tool takes an order_id
parameter and uses the database dependency to fetch the corresponding order, which is automatically parsed into our Order
Pydantic model. It then formats the order details into a clear, readable response. The agent can therefore use this tool to answer any questions about a specific order.
Let's see it in action:
USER: What's the status of my order 001?
AGENT: Your order (ID: 001) is currently confirmed. Here are the details:
- Order Date: December 19, 2024
- Item: Classic Noir Biker Jacket (M, Black)
- Total Amount: €1499.0
- Shipping Address: 123 Avenue des Champs-Élysées, Paris, 75008, France
#Shipping Address Updates
Our next tool handles shipping address updates, moving beyond retrieval to actual action execution:
Notice how the tool performs all necessary validations, verifying that the order exists and that it can be modified. Then it creates a new Address
object using our Pydantic model and uses the database dependency to update the address. Whether successful or not, it returns clear status messages that the agent can relay to the customer. Let's see some example conversations:
USER: I would like to update the shipping address for the order 002.
AGENT: Unfortunately, I am unable to update the shipping address for order 002, as it has already been delivered. If you need further assistance, please let me know.
USER: I would like to update the shipping address for the order 001.
AGENT: Could you please provide the new shipping address, including the street, city, postal code, and country?
USER: 14 Rue des Roses, 75008 Paris, France.
AGENT: The shipping address for your order 001 has been successfully updated to 14 Rue des Roses, 75008 Paris, France. If you need further assistance, feel free to ask.
#Handling Returns & Cancellations
One of the most challenging aspects of order management is handling returns and cancellations. Let's see how we can implement these tools:
PydanticAI makes it quite simple to handle the complexity of these operations elegantly and concisely. The agent automatically knows:
- Which parameters each operation requires (like return reasons and order IDs).
- When each tool is appropriate to use (based on order status and timing).
- How to handle error conditions (invalid states, expired windows).
- How to communicate results back to the customer (from success messages to explanations of why an operation can't proceed).
Let's see an example of a return request:
USER: My order is damaged and I would like to get a refund.
AGENT: I can assist you with processing a return for your damaged order. Could you please provide me with your order ID so I can help you further?
USER: 002.
AGENT: Your return request for the damaged order has been approved. You will receive a return label via email with ID RET-eedc4cdf4494. Please ship the item back within 14 days, ensuring it includes all original tags. Once we've received the item and verified its condition, your refund will be processed. If you need further assistance, please let me know.
#Human Escalation
Finally, a critical feature of any automated system is knowing when to hand over to human support. Our escalation tool will transfer conversations to a human during complex requests or unresolved issues, when the user specifically demands it, or when the agent detects customer dissatisfaction. The tool is also able to flag high-priority cases, ensuring urgent matters or clearly dissatisfied customers receive immediate attention.
Here's the implementation:
This implementation is simplified for demonstration purposes. A production system would need to integrate with your support desk software, trigger notifications, and possibly create support tickets with appropriate priority levels and routing. Those are additional details to add inside the tool, but the fundamental agent logic would remain the same.
Let's take a look at an example where the agent detects user frustration:
USER: I would like to cancel my order 002.
AGENT: Your order with ID 002 has already been delivered and cannot be canceled. However, if you wish to return the item, please let me know, and I can assist you with the return process.
USER: You are not being helpful.
AGENT: I've escalated your concern to our support team for further assistance. You will be contacted within 1 hour. Thank you for your patience.
Behind the scenes, the agent generates a detailed tool call that provides the support team with valuable context:
#Beyond the Demo: Production Considerations
While our example agents illustrate the core concepts, deploying AI agents in production requires additional considerations:
- Security, customer authentication and access control.
- Integration with your existing order management systems, inventory and customer databases.
- Agent performance monitoring and human feedback for continuous improvement.
- Logging and audit trails.
- Error handling and graceful degradation when external services are unavailable.
- GDPR compliance and proper data privacy.
If you're interested in exploring how AI agents and automations could transform your e-commerce operations, I'd love to have a conversation about your specific needs and challenges. You can reach me at guillermo@codeawake.com.