Dynamically creates a Pydantic model from a schema dictionary. :param param_schema: Schema with 'title', 'properties', and optionally 'required' keys. :return: A Pydantic model class for the defined schema. :raised ValueError: Invalid 'type' for property or recursive model creatio
(param_schema: t.Dict)
| 458 | |
| 459 | |
| 460 | def pydantic_model_from_param_schema(param_schema: t.Dict) -> t.Type: |
| 461 | """ |
| 462 | Dynamically creates a Pydantic model from a schema dictionary. |
| 463 | |
| 464 | :param param_schema: Schema with 'title', 'properties', and optionally 'required' keys. |
| 465 | :return: A Pydantic model class for the defined schema. |
| 466 | |
| 467 | :raised ValueError: Invalid 'type' for property or recursive model creation. |
| 468 | |
| 469 | Note: Requires global `schema_type_python_type_dict` for type mapping and |
| 470 | `fallback_values` for default values. |
| 471 | """ |
| 472 | required_fields = {} |
| 473 | optional_fields = {} |
| 474 | if "title" not in param_schema: |
| 475 | raise ValueError(f"Missing 'title' in param_schema: {param_schema}") |
| 476 | |
| 477 | param_title = str(param_schema["title"]).replace(" ", "") |
| 478 | required_props = param_schema.get("required", []) |
| 479 | |
| 480 | if param_schema.get("type") == "array": |
| 481 | # print("param_schema inside array - ", param_schema) |
| 482 | item_schema = param_schema.get("items") |
| 483 | if item_schema: |
| 484 | ItemType = t.cast( |
| 485 | t.Type, |
| 486 | json_schema_to_pydantic_type( |
| 487 | json_schema=item_schema, |
| 488 | ), |
| 489 | ) |
| 490 | return t.List[ItemType] # type: ignore |
| 491 | return t.List |
| 492 | |
| 493 | for prop_name, prop_info in param_schema.get("properties", {}).items(): |
| 494 | prop_type = prop_info.get("type") |
| 495 | prop_title = prop_info.get("title", prop_name).replace(" ", "") |
| 496 | prop_default = prop_info.get("default", FALLBACK_VALUES.get(prop_type)) |
| 497 | if ( |
| 498 | prop_type is not None |
| 499 | and prop_type in PYDANTIC_TYPE_TO_PYTHON_TYPE |
| 500 | and prop_type not in CONTAINER_TYPE |
| 501 | ): |
| 502 | signature_prop_type = PYDANTIC_TYPE_TO_PYTHON_TYPE[prop_type] |
| 503 | elif prop_type is None: |
| 504 | # Schema uses anyOf/allOf/oneOf/$ref instead of a top-level "type" key. |
| 505 | # Delegate to json_schema_to_pydantic_type which handles all combiners. |
| 506 | signature_prop_type = t.cast( |
| 507 | t.Type, |
| 508 | json_schema_to_pydantic_type(json_schema=prop_info), |
| 509 | ) |
| 510 | else: |
| 511 | signature_prop_type = pydantic_model_from_param_schema(prop_info) |
| 512 | |
| 513 | field_kwargs = { |
| 514 | "description": prop_info.get( |
| 515 | "description", prop_info.get("desc", prop_title) |
| 516 | ), |
| 517 | } |
searching dependent graphs…