LlamaIndex: How to Get Structured Data from LLMs
Key Takeaways
This video demonstrates how to get structured data from language models using LlamaIndex and Pydantic, allowing for predictable output with specific keys and value formats.
Full Transcript
hey everyone how's it going today's video very quick about how to get structured output out of your language model in Lama index okay now the idea behind this is that usually when you have your language model and you query it you're going to get just text from it okay now what happens if instead of getting text you want to get something like a Json file with very specific keys and value um formats that you want from that Json file okay your average language model is not going to be able to accurately get that just by prompting it okay so the idea that we have right here is that we're going to be initializing a schema using pantic adding it to our language model using Lama index and this is the language model that we're going to query and every time we're going to get a response with the schema that we have determined okay so let's take a look at how to do that [Music] okay so the first thing to do as I mentioned before is to create the schema of the data that we want to get in our case we're going to be using pantic in order to uh initialize our schema in case you're not familiar with it pantic is essentially just a library uh for python for high for data validation and essentially what it allows you to do is to create classes which are going to become the schemas of your data and you're going to be able to validate your data using these schemas okay um in this case I'm going to be asking my language model to create an album and an album is going to contain a name the name is always going to be a string an artist which is always going to be a string and a list of songs and each song is also going to have its own schema and it it's going to have a title and a length in seconds and the length in seconds is in integers okay now for the record this example comes straight from the documentation of L index um but let's actually take a look at this so I'm going to execute this and right here I'm going to go from L index core import a chat message and here as you can see I had previously initialized my language model let me just show you before because I was doing another tutorial before this one um so here I have initialized my language model from open AI okay so in this case I'm going to be using open ey for this I am using GPT 40 mini and this language model is the same one that I'm using right here however this is not the instance that I'm going to be quering like that I am actually going to going to be running as structured llm method on it and I'm going to Define this parameter right here output CLS as the uh output schema that I want my language model to return on every single location okay and in this case I am passing it the album schema that I created right here which contains a reference to the song schema as well okay and this I am going to be assigning it to a variable called s llm for structured llm and now anytime I am going to query this this language model right here I am going to get the response in the exact schema that I specialized right here so let's see I'm going to initialize a chat model um sorry a chat message from this string right here generate an example album for the film uh for the film uh who frame Roger Rabbit and let's see what it returns to us I am going to call the slm using the chat method and let's see now here we have the response and as you can see here the chat message is from the assistant and the content is not just the string of text it is actually the object that I want to get so here you have the name of the album is Who Framed Roger Rabbit the soundtrack um Roger Rabbit soundtrack sorry the artist is going to be various artists and then we have a list of songs okay so let's take a look at it under the microscope and here I'm for the record using pretty print I imported It Up Above in the dictionary in the notebook but essentially I'm using pretty print to print these objects more neatly but as you can see we have the artist right here various artists the name is who frame Roger Rabbit and the songs which are all of them I mean each one of them an element an instance of song which each one has a title and a length in seconds so there you go I mean as you can see this follows exactly the same um the same schema that we defined above right here so there we go that is how to use on how to use structured outputs in language models using L index let me know if you have any questions and let's continue with this course [Music]
Original Description
In this video, we’ll dive into how to get structured and predictable output from language models by leveraging LlamaIndex and Pydantic. Structured data is essential for applications where consistent formatting and data validation are key. Here, we’ll show you how to use a Pydantic model to enforce a schema, so that our Language Learning Model (LLM) generates responses that fit a specific structure.
---
Useful links:
👉 Code on this video: https://colab.research.google.com/drive/18rJ-BGN3-JVJtBGjslQ33M5liowbXkc4?usp=sharing
🚀 Become an AI Engineer with my cohort: https://course.alejandro-ao.com
---
☎️ Consulting for your company: https://link.alejandro-ao.com/consulting-call
❤️ Buy me a coffee... or a beer (thanks): https://link.alejandro-ao.com/l83gNq
💬 Join the Discord Help Server: https://link.alejandro-ao.com/HrFKZn
---
Connect with me
LinkedIn: https://www.linkedin.com/in/alejandro-ao/
X: https://twitter.com/_alejandroao
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Alejandro AO · Alejandro AO · 43 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
▶
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Linear Regression in R - Full Project for Beginners
Alejandro AO
Configure Webpack 5 in Wordpress (2025) with Typescript and SASS
Alejandro AO
R Programming 101 - Crash Course for beginners
Alejandro AO
Convert HTML template to WordPress Theme (2025) - Full Course
Alejandro AO
Javascript Interactive Map with Leaflet EASY (with Marker Clusters & Popups)
Alejandro AO
Vanilla JS Project: Multi Step form in HTML, CSS & OOP Javascript
Alejandro AO
How to do AJAX in WordPress correctly (2025)
Alejandro AO
React Leaflet Tutorial for Beginners (2025)
Alejandro AO
Linear Regression in Python - Full Project for Beginners
Alejandro AO
Logistic Regression Project: Cancer Prediction with Python
Alejandro AO
Display Equations in ChatGPT
Alejandro AO
Create a Chrome Extension (Manifest V3) for ChatGPT
Alejandro AO
Full-Stack Project | ChatGPT API, React, Node.js, Express
Alejandro AO
Streamlit Python Course: Build a Machine Learning App to Predict Cancer
Alejandro AO
Langchain PDF App (GUI) | Create a ChatGPT For Your PDF in Python
Alejandro AO
LangChain Memory Tutorial | Building a ChatGPT Clone in Python
Alejandro AO
Chat with a CSV | LangChain Agents Tutorial (Beginners)
Alejandro AO
Create a ChatGPT clone using Streamlit and LangChain
Alejandro AO
Chat with Multiple PDFs | LangChain App Tutorial in Python (Free LLMs and Embeddings)
Alejandro AO
Full Python Environment Setup for AI (or other) Apps + Virtual Environments
Alejandro AO
Langchain + Qdrant Cloud | Pinecone FREE Alternative (20GB) | Tutorial
Alejandro AO
LangChain Version 0.1 Explained | New Features & Changes
Alejandro AO
Create a RAG Chain using LangChain 0.1 (New version)
Alejandro AO
Tutorial | Chat with any Website using Python and Langchain (LATEST VERSION)
Alejandro AO
Deploy Your AI Streamlit App for FREE | Step-by-Step (Heroku Alternative)
Alejandro AO
What is Google's Gemini 1.5 Pro | 10 Million Token Window
Alejandro AO
Chat with MySQL Database with Python | LangChain Tutorial
Alejandro AO
Stream LLMs with LangChain + Streamlit | Tutorial
Alejandro AO
Chat with MySQL Database using GPT-4 and Mistral AI | Python GUI App
Alejandro AO
#1 Harrison Chase: LangChain and The Future of LLM Applications | Alejandro AO
Alejandro AO
CrewAI Step-by-Step | Complete Course for Beginners
Alejandro AO
Python: Automating a Marketing Team with AI Agents | Planning and Implementing CrewAI
Alejandro AO
Build a Web App (GUI) for your CrewAI Automation (Easy with Python)
Alejandro AO
Early days of RAG and LlamaIndex - Jerry Liu
Alejandro AO
LlamaParse: Convert PDF (with tables) to Markdown
Alejandro AO
#2 Jerry Liu - What is LlamaIndex, Agents & Advice for AI Engineers
Alejandro AO
CrewAI + Exa: Generate a Newsletter with Research Agents (Part 1)
Alejandro AO
#3 Joe Moura | Multi Agent Systems and CrewAI
Alejandro AO
Python: Create a ReAct Agent from Scratch
Alejandro AO
New Groq Models: Best for Function-Calling Agents
Alejandro AO
Introduction to LlamaIndex with Python (2025)
Alejandro AO
LlamaIndex: How to use LLMs
Alejandro AO
LlamaIndex: How to Get Structured Data from LLMs
Alejandro AO
Multimodal RAG: Chat with PDFs (Images & Tables) [2025]
Alejandro AO
Advanced RAG with LlamaIndex - Metadata Extraction [2025]
Alejandro AO
Learn MCP Servers with Python (EASY)
Alejandro AO
Create MCP Clients in JavaScript - Tutorial
Alejandro AO
Create an MCP Client in Python - FastAPI Tutorial
Alejandro AO
How to Build an MCP Client GUI with Streamlit and FastAPI
Alejandro AO
Vibe Coding For Engineers (make it ACTUALLY work)
Alejandro AO
LlamaExtract Tutorial: Convert PDF & Images into JSON
Alejandro AO
Local MCP Servers for Cursor (Step by step)
Alejandro AO
Anthropic: How to Build Multi Agent Systems
Alejandro AO
Deploy Remote MCP Servers in Python (Step by Step)
Alejandro AO
GPT-5 for Developers: API Changes, Pricing, Model Router & Security
Alejandro AO
Tutorial: Auth for Remote MCP Servers (Step by Step) | OAuth 2.1 with ScaleKit
Alejandro AO
Generate UI Tests with TestSprite MCP Server + TRAE
Alejandro AO
#4 Allan Guo | 19-yo YC Founder - Willow Voice
Alejandro AO
RAG Project: Build an AI Onboarding Chatbot with Streamlit, LangChain, and ChromaDB
Alejandro AO
MCP Security | Malicious MCP Servers (Protect Yourself)
Alejandro AO
More on: LLM Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Claude AI vs ChatGPT: Which One Is Actually Better in 2026?
Medium · AI
Claude AI vs ChatGPT: Which One Is Actually Better in 2026?
Medium · Programming
IntelliBooks: Classic RAG vs Graph RAG vs Agentic RAG – Choosing the Right AI Retrieval Architecture for Enterprise AI
Dev.to AI
Fluid, natural voice translation with Gemini 3.5 Live Translate
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI