Dynamic Pydantic Models for LlamaIndex

Exploring Dynamic Pydantic Class Creation for Custom User Queries in LlamaIndex

Kevin Wong
3 min readMar 25, 2024
(generated from wepik)

Introduction

In my previous post, https://medium.com/@kevinchwong/from-machine-learning-to-learning-machine-483eaa4b2855 I explored the synergy between LlamaIndex (LLM) and Pydantic models, leveraging Pydantic’s robustness to generate LLM outputs with specific predefined data types. Now, I aim to investigate the feasibility of dynamically creating Pydantic classes at runtime to accommodate custom user queries seamlessly.

Poetry setup

[tool.poetry.dependencies]
llama_index = "^0.9.48"
openai = "^1.14.3"
python = "^3.12.0"
pydantic = "^2.6.4"

Import

import json
from uuid import uuid4
from typing import List, Optional, Union
from pydantic import BaseModel, Field, create_model

Base Class Definition

# base class
class BaseLearnerNode(BaseModel):
asked: Optional[Union[str, List[str]]]
uuid: str = Field(default_factory=lambda: str(uuid4()))
> Base class schema:
{
"properties": {
"asked": {
"anyOf": [
{
"type": "string"
},
{
"items": {…

--

--