Pydantic AI · Chapter 6 of 23

Conversations

By the end of this chapter you can continue a conversation across runs with message_history, tell all_messages() from new_messages(), save a farmer's conversation as JSON and load it back with ModelMessagesTypeAdapter, trim old turns, and explain why the model itself remembers nothing. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Your first agents
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.

Why you need this

On WhatsApp a farmer rarely says everything at once. "What is the onion rate today?" "Which mandi?" "Lasalgaon." Ganesh answers threads like that all day, and he remembers the first message when the second arrives.

Every agent so far answered one message and forgot it. Run the agent on "Lasalgaon" alone and it has no idea a price was asked for. This chapter carries a conversation from one run to the next, keeps it when the server restarts, and keeps it from growing forever.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    two_turns.py: a two-turn chat

    Give an agent for openai-chat:lab-chat, with the instructions You are KhetBazaar's farmer assistant., a tool mandi_price (crop and mandi, returning khetbazaar.mandi_price). Run it on the farmer's first message: What is the tomato rate today? The model will ask which mandi. Then run it on the farmer's answer, Pimpalgaon, with the first run's messages as the history.

    Print the first output, the second output, then Messages: and the number of all the second run's messages, then New: and the number of its new messages.

  2. Task 2

    save_load.py: history saved and loaded

    With the same agent and tool, run the farmer's first message What is the grape rate today? and write the run's messages, as JSON, to conversation.json in the current folder. Print Saved: N messages, with the number of messages.

    Then, as a later run would, read conversation.json back into a list of messages with Pydantic AI's ModelMessagesTypeAdapter, and run the farmer's answer Nashik with that list as the history. Print the output.

  3. Task 3

    trim.py: keep the last turns only

    Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. and run it on four farmer messages, in order, as one conversation:

    My name is Rekha and I grow pomegranate in Sinnar.

    Is there rain coming this week?

    Do you charge any fee when you buy my crop?

    What is my name?

    Keep the whole history after each run, but send only its last 4 messages (the last two turns) as the history of the next run. Print each run's output on its own line.

  4. Task 4 · interview question

    Interview: remembers_nothing.py, the model remembers nothing

    Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. Run it on My name is Vijay and I grow grapes in Dindori. Then ask What is my name? twice: first with no history, then with the first run's messages as the history.

    Print Without history: and the first answer, then With history: and the second.

    The interviewer will ask where an agent's memory of a conversation really lives.

What you will learn

  • The model remembers nothing: every request carries the whole conversation.
  • message_history: continuing a conversation in the next run.
  • all_messages() and new_messages(): the whole conversation, or one run's part of it.
  • Saving and loading a conversation as JSON.
  • Trimming old turns, and what it costs.

More in Your first agents

Build "Conversations" yourself

KhetBazaar, a Nashik agritech startup, answers farmers on WhatsApp from a bike. You build its farmer assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.