Menu

Getting Reliable JSON from LLMs with Pydantic
📰

Getting Reliable JSON from LLMs with Pydantic

DEV Community: ai·Deependra Rajput·about 8 hours ago
#kQUMdovy
#devtoai#devto#dev#json#tickettriage
Long article (546 words) — no AI summary yet.
Reading 0:00
15s threshold

Asking an LLM to "reply in JSON" works most of the time. The rest of the time you get a friendly sentence before the JSON, a missing field, or a value your code doesn't expect, and your app crashes on json.loads .

Pydantic turns that "most of the time" into something you can rely on. Here's the pattern I use.

1. Define the output as a model

Write down exactly what a valid answer looks like, including the allowed values:

from typing import Literal

from pydantic import BaseModel , Field , ValidationError

class TicketTriage ( BaseModel ) : category : Literal [ "billing" , "bug" , "feature_request" , "other" ] priority : Literal [ "low" , "medium" , "high" ] summary : str = Field ( max_length = 200 )

Enter fullscreen mode

Exit fullscreen mode

This one class is now your documentation, your validator and your type hints.

2. Show the model the schema

Pydantic can generate a JSON Schema from the class, so the prompt and the validation can never drift apart:

import json

schema = json .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.