-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pydantic2 test runner * Add pydantic2 support * Fix poetry requirements syntax
- Loading branch information
1 parent
a1e68a3
commit 11efb9a
Showing
9 changed files
with
118 additions
and
90 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
Pydantic V1 and V2 compatibility layer. Pydantic V2 has a better API for the type inspection | ||
that we do in GPT-JSON, but we can easily bridge some of the concepts in V1. | ||
""" | ||
|
||
from typing import Type | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
def get_field_description(field): | ||
if hasattr(field, "description"): | ||
return field.description | ||
elif hasattr(field, "field_info"): | ||
return field.field_info.description | ||
else: | ||
raise ValueError(f"Unknown pydantic field class structure: {field}") | ||
|
||
|
||
def get_model_fields(model: Type[BaseModel]): | ||
if hasattr(model, "model_fields"): | ||
return model.model_fields | ||
elif hasattr(model, "__fields__"): | ||
return model.__fields__ | ||
else: | ||
raise ValueError(f"Unknown pydantic field class structure: {model}") | ||
|
||
|
||
def get_model_field_infos(model: Type[BaseModel]): | ||
if hasattr(model, "model_fields"): | ||
return model.model_fields | ||
elif hasattr(model, "__fields__"): | ||
return {key: value.field_info for key, value in model.__fields__.items()} # type: ignore | ||
else: | ||
raise ValueError(f"Unknown pydantic field class structure: {model}") |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.