Skip to content

Commit f4a9c7d

Browse files
committed
added to_pydantic_model as a method under the toolschema class
1 parent 8796452 commit f4a9c7d

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

packages/toolbox-core/src/toolbox_core/protocol.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# limitations under the License.
1414

1515
from inspect import Parameter
16-
from typing import Optional, Type
16+
from typing import Optional, Type, Any, cast
17+
18+
from pydantic import BaseModel, Field, create_model
1719

18-
from pydantic import BaseModel
1920

2021

2122
class ParameterSchema(BaseModel):
@@ -62,6 +63,18 @@ class ToolSchema(BaseModel):
6263
parameters: list[ParameterSchema]
6364
authRequired: list[str] = []
6465

66+
def to_pydantic_model(self) -> Type[BaseModel]:
67+
"""Converts the given manifest schema to a Pydantic BaseModel class."""
68+
field_definitions = {}
69+
for field in self.parameters:
70+
field_definitions[field.name] = cast(
71+
Any,
72+
(
73+
field.to_param().annotation,
74+
Field(description=field.description),
75+
),
76+
)
77+
return create_model("tool_model", **field_definitions)
6578

6679
class ManifestSchema(BaseModel):
6780
"""

packages/toolbox-core/src/toolbox_core/tool.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ async def __call__(self, *args: Any, **kwargs: Any) -> str:
171171
payload = all_args.arguments
172172

173173
# Perform argument type checks using pydantic
174-
model = _schema_to_model(self.__name__, self.__schema)
174+
model = self.__schema.to_pydantic_model()
175175
model.model_validate(payload)
176176

177177
# apply bounded parameters
@@ -304,18 +304,3 @@ def identify_required_authn_params(
304304
if required:
305305
required_params[param] = services
306306
return required_params
307-
308-
309-
def _schema_to_model(model_name: str, tool_schema: ToolSchema) -> Type[BaseModel]:
310-
"""Converts the given manifest schema to a Pydantic BaseModel class."""
311-
field_definitions = {}
312-
for field in tool_schema.parameters:
313-
field_definitions[field.name] = cast(
314-
Any,
315-
(
316-
field.to_param().annotation,
317-
Field(description=field.description),
318-
),
319-
)
320-
321-
return create_model(model_name, **field_definitions)

0 commit comments

Comments
 (0)