Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

unclecode/pydantype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

10 Commits

Repository files navigation

πŸš€ PydanType: Pydantic to TypedDict Converter

GitHub stars GitHub forks GitHub watchers License

Convert your Pydantic models to TypedDict with ease! πŸŽ‰

🌟 Motivation

Recently, Google Gemini introduced the ability to generate structured output, but here's the catch: unlike many environments that accept Pydantic models, they require TypeDict. It was tricky for me since I had a lot of Pydantic models in other projects, and I figured I wasn’t the only one. So, I created a simple utility that converts any Pydantic model to TypeDict, making it compatible with Gemini. Hopefully, this helps you as well! πŸ’‘

That's when this utility was born! Now you can:

  1. Define your models in Pydantic (with all its validation goodness) πŸ‘
  2. Convert them to TypedDict when needed (for APIs like Gemini) πŸ”„
  3. Enjoy the benefits of both! 🎊

πŸš€ Quick Start

Try it out instantly in our Colab notebook: Open In Colab

Install the package:

pip install pip install git+https://github.com/unclecode/pydantype.git

Use it in your code:

from pydantic import BaseModel
from pydantype import convert
class MyModel(BaseModel):
 name: str
 age: int
MyTypedDict = convert(MyModel)

🌈 Gemini 1.5 Pro Example

Here's how you can use this utility with Google's Gemini 1.5 Pro:

import google.generativeai as genai
from pydantic import BaseModel
from typing import List
from pydantype import convert
class Recipe(BaseModel):
 recipe_name: str
 ingredients: str
class RecipeList(BaseModel):
 recipes: List[Recipe]
RecipeListDict = convert(RecipeList)
model = genai.GenerativeModel('gemini-1.5-pro',
 generation_config={
 "response_mime_type": "application/json",
 "response_schema": RecipeListDict
 })
prompt = "List 3 popular cookie recipes"
response = model.generate_content(prompt)
print(response.text)

🎨 General Example

Here's a more general example showcasing various Pydantic features:

from typing import List, Optional
from pydantic import BaseModel, Field
from pydantype import convert
class Address(BaseModel):
 street: str
 city: str
 country: str = Field(default="Unknown")
class Person(BaseModel):
 name: str
 age: int
 address: Address
 hobbies: List[str] = []
 nickname: Optional[str] = None
PersonDict = convert(Person)
# PersonDict is now a TypedDict with the same structure as Person

πŸ›  Features

  • Converts simple and complex Pydantic models πŸ—
  • Handles nested models, lists, and dictionaries πŸ”„
  • Supports optional fields and unions 🀝
  • Preserves type hints for better static analysis πŸ”

🀝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests. πŸ™Œ

πŸ“œ License

This project is licensed under the MIT License. See the LICENSE file for details.


Happy coding! 🎈🎊

About

A library to convert Pydantic models to TypedDict

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /