How to use Pydantic with OpenRouter API

This page lists a series of tutorials for using Pydantic with different LLMs on OpenRouter

Three things to note:

1 While OpenRouter does provide a way to specify the schema as one of the API parameters, I have not had much luck getting it to work with the LLMs I have tried. It could have been an error on my part, but there are two more points to consider.

2 Not all LLMs support structured outputs (via OpenRouter’s API), but you can hard code the JSON schema into the prompt itself to get around this problem

3 It is much easier to customize and “engineer” the prompt using my system compared to the API parameter exposed by OpenRouter

Basic Setup

This is the structure of the prompt

prompt = f"""
{system_instruction} 

Report: {input_text}

JSON Schema:
{json_schema_text}
"""

Notice that there are three parts to the prompt:

  • The system instruction
  • The input text
  • The response schema we expect

This is the system instruction

Here is the input text

This is the schema I am using

This basic setup applies to all the tutorials listed below

While you should be able to follow along with all the tutorials by reading the code I have published (and getting a bit of help from some LLM if necessary), you can also download the entire Python project from the course below (open the project in PyCharm inside a venv and pip install the requirements)

Tutorials

Using Pydantic with DeepSeek V3.1 Terminus on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using DeepSeek V3.1 Terminus. Please read this tutorial first to understand the basic setup Code: import os import json import requests import datetime from inputs import get_input_text from system import system_instruction_1 as system_instruction from schema import VAERSReport …
Using Pydantic with OpenGVLab: InternVL3 78B on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using OpenGVLab: InternVL3 78B. Please read this tutorial first to understand the basic setup Code: import os import json import requests import time from inputs import get_input_text from system import system_instruction_1 as system_instruction from schema import VAERSReport …
Using Pydantic with Arcee AI: AFM 4.5B on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using Arcee AI: AFM 4.5B. Please read this tutorial first to understand the basic setup Code: import os import json import requests import time from inputs import get_input_text from system import system_instruction_1 as system_instruction from schema import …
Using Pydantic with Qwen3 Coder Plus on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using Qwen3 Coder Plus. Please read this tutorial first to understand the basic setup Code: import os import json import requests import time from inputs import get_input_text from system import system_instruction_1 as system_instruction from schema import VAERSReport …
Using Pydantic with Qwen3 Coder Flash on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using Qwen3 Coder Flash. Please read this tutorial first to understand the basic setup Code: import os import json import requests import time from inputs import get_input_text from system import system_instruction_1 as system_instruction from schema import VAERSReport …
Using Pydantic with xAI Grok 4 Fast on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using xAI Grok 4 Fast. The code uses the free model, but you can just change the model name to the paid one once it becomes available. Please read this tutorial first to understand the basic setup …