Skip to content

Commit 147e0e4

Browse files
committed
improve file class
1 parent 7f8c5a9 commit 147e0e4

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/inferencesh/models/file.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import hashlib
88
from pathlib import Path
99
from tqdm import tqdm
10+
from pydantic_core import core_schema
11+
from pydantic_core.core_schema import GetJsonSchemaHandler, JsonSchemaValue
1012

1113

1214
class File(BaseModel):
@@ -239,21 +241,24 @@ def refresh_metadata(self) -> None:
239241
@classmethod
240242
def model_json_schema(cls, **kwargs):
241243
schema = super().model_json_schema(**kwargs)
244+
schema["$id"] = "/schemas/File"
242245
# Create a schema that accepts either a string or the full object
243-
base_schema = {
244-
"anyOf": [
246+
return {
247+
"oneOf": [
245248
{"type": "string"}, # Accept string input
246-
{
247-
"type": "object",
248-
"properties": schema["properties"],
249-
"title": schema.get("title", "File"),
250-
"description": "A class representing a file in the inference.sh ecosystem."
251-
}
249+
schema # Accept full object input
250+
]
251+
}
252+
253+
@classmethod
254+
def __get_pydantic_json_schema__(
255+
cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
256+
) -> JsonSchemaValue:
257+
schema = handler(core_schema) # this is the "normal" schema
258+
return {
259+
"oneOf": [
260+
{"type": "string"},
261+
schema
252262
]
253263
}
254-
# If this is the top-level schema, return as is
255-
if not kwargs.get("ref_template"):
256-
return base_schema
257-
# If this is being used in $defs, include it in the schema
258-
schema.update(base_schema)
259-
return schema
264+

0 commit comments

Comments
 (0)