Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Predictable Output

00:00 So far, you’ve created a Python assistant that only answers Python-related questions but gives you unstructured output. How can you fix this problem? There are two main ways to structure the output you receive from Claude.

00:13 The first one is hand-written JSON Schema, and the second one is using Pydantic. You won’t spend much time with JSON schemas in this course, but let’s take a look at how it looks like in this following example.

00:26 Here you have output_config that lets you define the exact structure of Claude’s response, so it returns predictable machine-readable JSON that follows your schema. And the schema includes three fields.

00:41 Take a look at the fields that the response should contain. You have function_name, that should be a string. You have code, which should be a string.

00:49 And you have explanation, which is also a string. By using the required parameter, you’re saying that all three must be included in every response. And by including additionalProperties :False, you’re preventing Claude from returning any fields that aren’t defined in the schema.

01:08 JSON schemas are language-agnostic, meaning all languages understand JSON, and they’re very customizable, which is a good thing. But many Python developers actually prefer using Pydantic.

01:20 Pydantic is a Python library that validates, parses, and structures data using Python type annotations. So it’s natively Python. Here at Real Python, we have both a tutorial and a video course for you to understand Pydantic.

01:35 They’re both called Pydantic: Simplifying Data Validation in Python. If you want to learn more, check them out. The reason Python developers often prefer Pydantic is that it’s more concise and easier to read.

01:48 It uses Python’s type hints, so you don’t have to learn another schema language. It validates responses automatically, and it integrates well with IDEs, static type checkers, and frameworks like FastAPI.

02:01 Your task is to structure the output using Pydantic so that it is a FunctionDescription object. You’re going to see how to do that in a second. You want the responses to always have three fields, function_name, code, and explanation, and they should all be strings.

02:19 Go ahead and create a new file called structured_output.py. You can copy-paste the code from the last iteration of coding_assistant.py.

02:28 And now it’s time to make the needed changes. Make sure you have Pydantic installed in your virtual environment. First of all, you need to import BaseModel from Pydantic.

02:39 So from pydantic

02:43 import BaseModel.

02:47 Now before your client object, go ahead and create a class called FunctionDescription. You’re inheriting from BaseModel.

02:59 And you want the three fields, so function_name is the first one. Make sure you’re saying it’s a string. Then you want your code, and you want that also to be a string.

03:14 And you want explanation to also be a string. Perfect. Now you want to send the request to Claude and automatically parse the response into the Pydantic model. To do that, look at line 16.

03:30 It says response = client.messages.create(). Instead of create(), you can replace it with parse().

03:39 And then right after you’re closing your bracket, make sure you include a comma here.

03:46 And here you want to specify how you want your output format to be. So go ahead and say output_format

03:55 equals the class you just created that inherits from BaseModel. So FunctionDescription. Don’t forget your comma.

04:06 So far you have output_format = FunctionDescription, which tells Claude to format its response according to the FunctionDescription model.

04:15 Now after this, you have to retrieve the parsed response as a FunctionDescription object. So you can access each field as an attribute.

04:25 Go ahead and write result = response.parsed_output.

04:35 You can get each field and then print that. As you can see, you’re getting result.function_name as the function. You’re getting result.code as the code and result.explanation for the explanation. And that is all you need to change.

04:51 So you can go ahead, head to your terminal, and run this file.

04:57 You can ask the same question from before. How do I reverse a list?

05:08 And here you go. Look at the response. It’s way more structured than before. You have your function name in the beginning. You’ll get the code after. You even have an example usage here.

05:19 And finally, you get the full explanation as a string. Congratulations. You just created a Python coding assistant that refuses to answer unrelated questions and also gives you structured output that is predictable to store or use in other parts of your application.

05:38 Now it’s time to go ahead and look at how you can troubleshoot the most common errors and also what are your next steps.

Become a Member to join the conversation.