-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Any, Dict, Type, cast | ||
|
||
from pydantic import BaseModel, ConfigDict, RootModel | ||
from pydantic.json_schema import ( | ||
DEFAULT_REF_TEMPLATE, | ||
GenerateJsonSchema, | ||
JsonSchemaMode, | ||
) | ||
|
||
|
||
def _create_root_model(name: str, type_: Any) -> Type[RootModel]: | ||
"""Create a base class.""" | ||
|
||
def schema( | ||
cls: Type[BaseModel], | ||
by_alias: bool = True, | ||
ref_template: str = DEFAULT_REF_TEMPLATE, | ||
) -> Dict[str, Any]: | ||
# Complains about schema not being defined in superclass | ||
schema_ = super(cls, cls).schema( # type: ignore[misc] | ||
by_alias=by_alias, ref_template=ref_template | ||
) | ||
schema_["title"] = name | ||
return schema_ | ||
|
||
def model_json_schema( | ||
cls: Type[BaseModel], | ||
by_alias: bool = True, | ||
ref_template: str = DEFAULT_REF_TEMPLATE, | ||
schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema, | ||
mode: JsonSchemaMode = "validation", | ||
) -> Dict[str, Any]: | ||
# Complains about model_json_schema not being defined in superclass | ||
schema_ = super(cls, cls).model_json_schema( # type: ignore[misc] | ||
by_alias=by_alias, | ||
ref_template=ref_template, | ||
schema_generator=schema_generator, | ||
mode=mode, | ||
) | ||
schema_["title"] = name | ||
return schema_ | ||
|
||
base_class_attributes = { | ||
"__annotations__": {"root": type_}, | ||
"model_config": ConfigDict(arbitrary_types_allowed=True), | ||
"schema": classmethod(schema), | ||
"model_json_schema": classmethod(model_json_schema), | ||
# Should replace __module__ with caller based on stack frame. | ||
"__module__": "langserve._pydantic", | ||
} | ||
|
||
custom_root_type = type(name, (RootModel,), base_class_attributes) | ||
return cast(Type[RootModel], custom_root_type) |
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