Created
July 16, 2025 08:47
-
-
Save intellectronica/d8830c18c6e77585691717478f397429 to your computer and use it in GitHub Desktop.
pydantic-xml-structured-inputs.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyOGLBlS85n6N8qig7qfU8VO", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/intellectronica/d8830c18c6e77585691717478f397429/pydantic-xml-structured-inputs.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8d4f592d" | |
}, | |
"source": [ | |
"%pip install pydantic pydantic-ai email-validator\n", | |
"from IPython.display import clear_output ; clear_output()" | |
], | |
"execution_count": 21, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "5c10b446", | |
"outputId": "9396dab3-4bc9-4acc-f52c-e08efc03e040" | |
}, | |
"source": [ | |
"from typing import List, Dict, Optional\n", | |
"from pydantic import BaseModel, Field, EmailStr\n", | |
"from pydantic_ai import format_as_xml\n", | |
"\n", | |
"class Address(BaseModel):\n", | |
" street: str\n", | |
" city: str\n", | |
" zip_code: str\n", | |
"\n", | |
"class Interest(BaseModel):\n", | |
" name: str\n", | |
" level: int\n", | |
"\n", | |
"class Person(BaseModel):\n", | |
" id: int\n", | |
" name: str\n", | |
" age: int\n", | |
" email: EmailStr\n", | |
" address: Address\n", | |
" interests: List[Interest]\n", | |
" metadata: Dict[str, str] = {}\n", | |
"\n", | |
"person_instance = Person(\n", | |
" id=456,\n", | |
" name=\"Jane Smith\",\n", | |
" age=25,\n", | |
" email=\"[email protected]\",\n", | |
" address=Address(\n", | |
" street=\"456 Oak Ave\",\n", | |
" city=\"Otherville\",\n", | |
" zip_code=\"67890\"\n", | |
" ),\n", | |
" interests=[\n", | |
" Interest(name=\"Painting\", level=5),\n", | |
" Interest(name=\"Gardening\", level=3)\n", | |
" ],\n", | |
" metadata={\"group\": \"Students\"}\n", | |
")\n", | |
"\n", | |
"prompt = f\"\"\"Here is information about a person:\n", | |
"\n", | |
"{format_as_xml(person_instance, root_tag='person')}\n", | |
"\n", | |
"Write a brief summary about this person based on the information provided.\n", | |
"\"\"\"\n", | |
"\n", | |
"print(prompt)" | |
], | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Here is information about a person:\n", | |
"\n", | |
"<person>\n", | |
" <id>456</id>\n", | |
" <name>Jane Smith</name>\n", | |
" <age>25</age>\n", | |
" <email>[email protected]</email>\n", | |
" <address>\n", | |
" <street>456 Oak Ave</street>\n", | |
" <city>Otherville</city>\n", | |
" <zip_code>67890</zip_code>\n", | |
" </address>\n", | |
" <interests>\n", | |
" <example>\n", | |
" <name>Painting</name>\n", | |
" <level>5</level>\n", | |
" </example>\n", | |
" <example>\n", | |
" <name>Gardening</name>\n", | |
" <level>3</level>\n", | |
" </example>\n", | |
" </interests>\n", | |
" <metadata>\n", | |
" <group>Students</group>\n", | |
" </metadata>\n", | |
"</person>\n", | |
"\n", | |
"Write a brief summary about this person based on the information provided.\n", | |
"\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "zcSmeOenYHfK" | |
}, | |
"execution_count": 22, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment