GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - unclecode/pydantype: A library to convert Pydantic models to TypedDict

BranchesTags
Open more actions menu

Latest commit

 

History

10 Commits
10 Commits

Folders and files

NameName
Last commit message
Last commit date
examples
examples
 
 
src/pydantype
src/pydantype
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
requirements.txt
requirements.txt
 
 
setup.py
setup.py
 
 

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

Readme
MIT license
Activity

Stars

42 stars

Watchers

6 watching

Forks

8 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.