Create a Pydantic model from a parameter schema. Args: name: Name for the model (will be converted to PascalCase + "Input") schema: Parameter schema dictionary Returns: Pydantic BaseModel class
(self, name: str, schema: Dict[str, Any])
| 229 | |
| 230 | |
| 231 | def build_args_schema(self, name: str, schema: Dict[str, Any]) -> Type[BaseModel]: |
| 232 | """Create a Pydantic model from a parameter schema. |
| 233 | |
| 234 | Args: |
| 235 | name: Name for the model (will be converted to PascalCase + "Input") |
| 236 | schema: Parameter schema dictionary |
| 237 | |
| 238 | Returns: |
| 239 | Pydantic BaseModel class |
| 240 | """ |
| 241 | properties = schema.get("properties", {}) |
| 242 | required = set(schema.get("required", [])) |
| 243 | |
| 244 | model_name = inflection.camelize(name) + "Input" |
| 245 | |
| 246 | if not properties: |
| 247 | return create_model( |
| 248 | model_name, |
| 249 | __config__=ConfigDict(arbitrary_types_allowed=True, extra="allow"), |
| 250 | ) |
| 251 | |
| 252 | field_definitions: Dict[str, Any] = {} |
| 253 | for param_name, param_info in properties.items(): |
| 254 | python_type_str = param_info.get(PYTHON_TYPE_FIELD) |
| 255 | if python_type_str: |
| 256 | python_type = self.parse_type_string(python_type_str) |
| 257 | else: |
| 258 | json_type = param_info.get("type", "string") |
| 259 | python_type = self.json_type_to_python_type(json_type) |
| 260 | |
| 261 | is_required = param_name in required |
| 262 | if "default" in param_info: |
| 263 | default_value = param_info["default"] |
| 264 | elif is_required: |
| 265 | default_value = ... # Required |
| 266 | else: |
| 267 | default_value = None |
| 268 | |
| 269 | description = param_info.get("description", "") |
| 270 | if is_required and default_value is ...: |
| 271 | field_definitions[param_name] = ( |
| 272 | python_type, |
| 273 | Field(description=description), |
| 274 | ) |
| 275 | else: |
| 276 | field_definitions[param_name] = ( |
| 277 | Optional[python_type] if not is_required else python_type, |
| 278 | Field(default=default_value, description=description), |
| 279 | ) |
| 280 | |
| 281 | return create_model( |
| 282 | model_name, |
| 283 | __config__=ConfigDict(arbitrary_types_allowed=True, extra="allow"), |
| 284 | **field_definitions, |
| 285 | ) |
| 286 | |
| 287 | def build_function_calling(self, name: str, description: str, schema: Dict[str, Any]) -> Dict[str, Any]: |
| 288 | """Build OpenAI-compatible function-calling representation. |
no test coverage detected