Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """ |
| 2 | Shared utils. |
| 3 | """ |
| 4 | |
| 5 | import copy |
| 6 | import dataclasses |
| 7 | import hashlib |
| 8 | import json |
| 9 | import keyword |
| 10 | import typing as t |
| 11 | import uuid |
| 12 | from functools import reduce |
| 13 | from inspect import Parameter |
| 14 | |
| 15 | from pydantic import BaseModel, Field, create_model |
| 16 | from pydantic.fields import FieldInfo |
| 17 | |
| 18 | from composio.exceptions import InvalidParams, InvalidSchemaError |
| 19 | from composio.utils.json_schema import dereference_json_schema |
| 20 | from composio.utils.logging import get as get_logger |
| 21 | from composio.utils.schema_converter import ( |
| 22 | _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, |
| 23 | CONTAINER_TYPE, |
| 24 | FALLBACK_VALUES, |
| 25 | PYDANTIC_TYPE_TO_PYTHON_TYPE, |
| 26 | _mark_explicit_default_fields, |
| 27 | _with_exact_validation, |
| 28 | apply_object_policy, |
| 29 | json_schema_to_pydantic_type, |
| 30 | ) |
| 31 | |
| 32 | logger = get_logger(__name__) |
| 33 | |
| 34 | # Re-export for backward compatibility |
| 35 | __all__ = [ |
| 36 | "json_schema_to_pydantic_type", |
| 37 | "PYDANTIC_TYPE_TO_PYTHON_TYPE", |
| 38 | "CONTAINER_TYPE", |
| 39 | "FALLBACK_VALUES", |
| 40 | "json_schema_to_pydantic_field", |
| 41 | "json_schema_to_fields_dict", |
| 42 | "json_schema_to_model", |
| 43 | "pydantic_model_from_param_schema", |
| 44 | "get_signature_format_from_schema_params", |
| 45 | "get_pydantic_signature_format_from_schema_params", |
| 46 | "generate_request_id", |
| 47 | "ToolSchemaAliases", |
| 48 | "alias_tool_input_schema", |
| 49 | "restore_tool_arguments", |
| 50 | "substitute_reserved_python_keywords", |
| 51 | "reinstate_reserved_python_keywords", |
| 52 | "normalize_tool_arguments", |
| 53 | "validate_and_serialize_tool_arguments", |
| 54 | ] |
| 55 | |
| 56 | reserved_names = ["validate"] |
| 57 | |
| 58 | _OBJ_MARKER = "-_object_-" |
| 59 | _ARR_MARKER = "-_array_-" |
| 60 | _MAX_PROVIDER_ALIAS_LENGTH = 64 |
| 61 | _MISSING_ARGUMENT = object() |
| 62 | |
| 63 | |
| 64 | def normalize_tool_arguments(arguments: t.Any) -> t.Dict[str, t.Any]: |
| 65 | """Coerce model-supplied tool arguments into a dict. |
| 66 | |
| 67 | Models (and some MCP transports) occasionally emit tool-call arguments as a |
| 68 | JSON string instead of a dict, which breaks execution with errors such as |
| 69 | ``tool_use.input: Input should be a valid dictionary``. This is the single |
| 70 | coercion every provider routes through so behaviour is identical everywhere. |
| 71 | |
| 72 | See https://github.com/ComposioHQ/composio/issues/2406. |
| 73 | |
| 74 | - ``None`` becomes ``{}`` (some models send no arguments for no-arg tools). |
| 75 | - A dict is returned unchanged. |
| 76 | - A string is JSON-parsed; an empty / whitespace-only string becomes ``{}``. |
| 77 | - Anything that does not resolve to a dict (lists, primitives, unparseable |
| 78 | strings, JSON that parses to a non-object) raises :class:`InvalidParams`. |
| 79 | |
| 80 | :param arguments: Raw arguments as received from the model / framework. |
| 81 | :return: The normalized arguments as a dict. |
| 82 | :raises InvalidParams: If the arguments cannot be resolved to a dict. |
| 83 | """ |
| 84 | if arguments is None: |
| 85 | return {} |
| 86 | |
| 87 | if isinstance(arguments, str): |
| 88 | stripped = arguments.strip() |
| 89 | if not stripped: |
| 90 | return {} |
| 91 | try: |
| 92 | parsed = json.loads(stripped) |
| 93 | except json.JSONDecodeError as e: |
| 94 | raise InvalidParams( |
| 95 | f"Tool arguments were provided as a string that is not valid JSON: {e}" |
| 96 | ) from e |
| 97 | return _as_dict(parsed) |
| 98 | |
| 99 | return _as_dict(arguments) |
| 100 | |
| 101 | |
| 102 | def _as_dict(value: t.Any) -> t.Dict[str, t.Any]: |
| 103 | if isinstance(value, dict): |
| 104 | return value |
| 105 | raise InvalidParams( |
| 106 | f"Tool arguments must resolve to an object, received {type(value).__name__}" |
| 107 | ) |
| 108 | |
| 109 | |
| 110 | def validate_and_serialize_tool_arguments( |
| 111 | args_schema: t.Type[BaseModel], |
| 112 | arguments: t.Dict[str, t.Any], |
| 113 | ) -> t.Dict[str, t.Any]: |
| 114 | """Validate provider arguments and serialize only values the backend should see. |
| 115 | |
| 116 | Pydantic models use ``None`` for optional fields that were not supplied, so a |
| 117 | plain ``model_dump()`` changes omission into an explicit null. Conversely, |
| 118 | ``exclude_unset=True`` also drops defaults declared by the source JSON Schema. |
| 119 | Generated models track fields with declared defaults. Combining that metadata |
| 120 | recursively with each selected model's ``model_fields_set`` preserves aliases, |
| 121 | explicit nulls, defaults, and validated dynamic extras without replaying the |
| 122 | source schema during execution. |
| 123 | """ |
| 124 | validated = args_schema.model_validate(arguments) |
| 125 | return t.cast( |
| 126 | t.Dict[str, t.Any], |
| 127 | _serialize_present_arguments( |
| 128 | validated, |
| 129 | validated.model_dump(mode="python", by_alias=True), |
| 130 | arguments, |
| 131 | ), |
| 132 | ) |
| 133 | |
| 134 | |
| 135 | def _model_field_aliases( |
| 136 | model: t.Type[BaseModel], |
| 137 | ) -> t.Dict[str, t.Tuple[str, FieldInfo]]: |
| 138 | fields = {} |
| 139 | for name, field in model.model_fields.items(): |
| 140 | alias = field.serialization_alias or field.alias |
| 141 | fields[alias if isinstance(alias, str) else name] = (name, field) |
| 142 | return fields |
| 143 | |
| 144 | |
| 145 | def _serialize_present_arguments( |
| 146 | validated: t.Any, |
| 147 | serialized: t.Any, |
| 148 | supplied: t.Any, |
| 149 | ) -> t.Any: |
| 150 | """Recursively remove values Pydantic added for omitted optional fields.""" |
| 151 | if supplied is _MISSING_ARGUMENT: |
| 152 | return serialized |
| 153 | |
| 154 | if isinstance(validated, BaseModel) and isinstance(serialized, dict): |
| 155 | fields = _model_field_aliases(type(validated)) |
| 156 | included_fields = set(validated.model_fields_set) |
| 157 | included_fields.update( |
| 158 | getattr(type(validated), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ()) |
| 159 | ) |
| 160 | supplied_values = supplied if isinstance(supplied, dict) else {} |
| 161 | result: t.Dict[str, t.Any] = {} |
| 162 | for key, value in serialized.items(): |
| 163 | field = fields.get(key) |
| 164 | if field is not None: |
| 165 | name, _ = field |
| 166 | if name not in included_fields: |
| 167 | continue |
| 168 | child_validated = getattr(validated, name) |
| 169 | if key in supplied_values: |
| 170 | child_supplied = supplied_values[key] |
| 171 | elif name in supplied_values: |
| 172 | child_supplied = supplied_values[name] |
| 173 | else: |
| 174 | child_supplied = _MISSING_ARGUMENT |
| 175 | else: |
| 176 | extra = validated.model_extra |
| 177 | if extra is None or key not in extra: |
| 178 | continue |
| 179 | child_validated = extra[key] |
| 180 | child_supplied = supplied_values.get(key, _MISSING_ARGUMENT) |
| 181 | result[key] = _serialize_present_arguments( |
| 182 | child_validated, |
| 183 | value, |
| 184 | child_supplied, |
| 185 | ) |
| 186 | return result |
| 187 | |
| 188 | if isinstance(serialized, dict): |
| 189 | supplied_values = supplied if isinstance(supplied, dict) else {} |
| 190 | validated_values = validated if isinstance(validated, dict) else {} |
| 191 | return { |
| 192 | key: _serialize_present_arguments( |
| 193 | validated_values.get(key, _MISSING_ARGUMENT), |
| 194 | value, |
| 195 | supplied_values.get(key, _MISSING_ARGUMENT), |
| 196 | ) |
| 197 | for key, value in serialized.items() |
| 198 | if key in supplied_values or key in validated_values |
| 199 | } |
| 200 | |
| 201 | if isinstance(serialized, (list, tuple)): |
| 202 | supplied_items = supplied if isinstance(supplied, (list, tuple)) else () |
| 203 | validated_items = validated if isinstance(validated, (list, tuple)) else () |
| 204 | items = [ |
| 205 | _serialize_present_arguments( |
| 206 | validated_items[index] |
| 207 | if index < len(validated_items) |
| 208 | else _MISSING_ARGUMENT, |
| 209 | value, |
| 210 | supplied_items[index] |
| 211 | if index < len(supplied_items) |
| 212 | else _MISSING_ARGUMENT, |
| 213 | ) |
| 214 | for index, value in enumerate(serialized) |
| 215 | ] |
| 216 | return tuple(items) if isinstance(serialized, tuple) else items |
| 217 | |
| 218 | return serialized |
| 219 | |
| 220 | |
| 221 | def _make_safe_name(name: str) -> str: |
| 222 | """Append ``_rs`` to a Python keyword so it can be used as a parameter name.""" |
| 223 | return f"{name}_rs" |
| 224 | |
| 225 | |
| 226 | def _make_python_identifier(name: str) -> str: |
| 227 | if keyword.iskeyword(name): |
| 228 | return _make_safe_name(name) |
| 229 | |
| 230 | safe = "".join( |
| 231 | char if (char.isascii() and (char.isalnum() or char == "_")) else "_" |
| 232 | for char in name |
| 233 | ) |
| 234 | if not safe: |
| 235 | safe = "param" |
| 236 | if safe[0].isdigit() or safe[0] == "_": |
| 237 | safe = f"param_{safe.lstrip('_') or 'value'}" |
| 238 | if keyword.iskeyword(safe): |
| 239 | safe = _make_safe_name(safe) |
| 240 | if len(safe) > _MAX_PROVIDER_ALIAS_LENGTH: |
| 241 | hash_suffix = hashlib.sha256(name.encode()).hexdigest()[:8] |
| 242 | safe = ( |
| 243 | f"{safe[: _MAX_PROVIDER_ALIAS_LENGTH - len(hash_suffix) - 1]}_{hash_suffix}" |
| 244 | ) |
| 245 | return safe |
| 246 | |
| 247 | |
| 248 | @dataclasses.dataclass(frozen=True) |
| 249 | class ToolSchemaAliases: |
| 250 | """Provider-visible schema plus mapping back to backend argument names.""" |
| 251 | |
| 252 | schema: t.Dict[str, t.Any] |
| 253 | aliases: t.Dict[str, t.Any] |
| 254 | |
| 255 | def restore_arguments(self, arguments: dict) -> dict: |
| 256 | return restore_tool_arguments(arguments, self.aliases) |
| 257 | |
| 258 | |
| 259 | def alias_tool_input_schema(schema: t.Dict) -> ToolSchemaAliases: |
| 260 | """Alias tool input schema keys so they are valid Python parameter names. |
| 261 | |
| 262 | Returns a :class:`ToolSchemaAliases` object containing a deep-copied schema |
| 263 | for provider/framework exposure and a reverse alias map for restoring model |
| 264 | arguments before calling the backend executor. |
| 265 | |
| 266 | Python keywords keep the historical ``_rs`` suffix (``from`` becomes |
| 267 | ``from_rs``). Other names that cannot be used as Python identifiers are |
| 268 | converted to Pydantic-safe identifiers and long aliases are capped at 64 |
| 269 | characters so the same provider-facing schema is accepted by |
| 270 | Anthropic-style tool schema validators. Internal JSON Schema references are |
| 271 | inlined before aliasing so referenced object properties are exposed through |
| 272 | the same safe names. If two properties would expose the same alias, an |
| 273 | :class:`InvalidSchemaError` is raised instead of guessing. |
| 274 | """ |
| 275 | aliased_schema = t.cast( |
| 276 | t.Dict[str, t.Any], |
| 277 | dereference_json_schema( |
| 278 | copy.deepcopy(schema), |
| 279 | on_unresolved="sentinel", |
| 280 | ), |
| 281 | ) |
| 282 | schema_params, aliases = _alias_schema_properties(aliased_schema) |
| 283 | return ToolSchemaAliases(schema=schema_params, aliases=aliases) |
| 284 | |
| 285 | |
| 286 | def _alias_schema_properties(schema: t.Dict[str, t.Any]) -> t.Tuple[dict, dict]: |
| 287 | properties = schema.get("properties") |
| 288 | if not isinstance(properties, dict): |
| 289 | return schema, {} |
| 290 | |
| 291 | aliases: t.Dict[str, t.Any] = {} |
| 292 | aliased_properties: t.Dict[str, t.Any] = {} |
| 293 | |
| 294 | for original_name, property_schema in properties.items(): |
| 295 | safe_name = _make_python_identifier(original_name) |
| 296 | if safe_name in aliased_properties: |
| 297 | raise InvalidSchemaError( |
| 298 | "Tool input schema property names produce a duplicate Python " |
| 299 | f"parameter alias {safe_name!r}" |
| 300 | ) |
| 301 | |
| 302 | nested_object_aliases: t.Dict[str, t.Any] = {} |
| 303 | nested_array_aliases: t.Dict[str, t.Any] = {} |
| 304 | if isinstance(property_schema, dict): |
| 305 | property_schema, nested_object_aliases = _alias_nested_object_schema( |
| 306 | property_schema |
| 307 | ) |
| 308 | property_schema, nested_array_aliases = _alias_nested_array_schema( |
| 309 | property_schema |
| 310 | ) |
| 311 | |
| 312 | aliased_properties[safe_name] = property_schema |
| 313 | if safe_name != original_name or nested_object_aliases or nested_array_aliases: |
| 314 | aliases[safe_name] = original_name |
| 315 | if nested_object_aliases: |
| 316 | aliases[f"{safe_name}{_OBJ_MARKER}"] = nested_object_aliases |
| 317 | if nested_array_aliases: |
| 318 | aliases[f"{safe_name}{_ARR_MARKER}"] = nested_array_aliases |
| 319 | |
| 320 | schema["properties"] = aliased_properties |
| 321 | if aliases and "required" in schema: |
| 322 | reverse = { |
| 323 | original: safe |
| 324 | for safe, original in aliases.items() |
| 325 | if not safe.endswith(_OBJ_MARKER) and not safe.endswith(_ARR_MARKER) |
| 326 | } |
| 327 | schema["required"] = [reverse.get(r, r) for r in schema["required"]] |
| 328 | |
| 329 | return schema, aliases |
| 330 | |
| 331 | |
| 332 | def _alias_nested_object_schema( |
| 333 | property_schema: t.Dict[str, t.Any], |
| 334 | ) -> t.Tuple[dict, dict]: |
| 335 | if not isinstance(property_schema.get("properties"), dict): |
| 336 | return property_schema, {} |
| 337 | return _alias_schema_properties(property_schema) |
| 338 | |
| 339 | |
| 340 | def _alias_nested_array_schema( |
| 341 | property_schema: t.Dict[str, t.Any], |
| 342 | ) -> t.Tuple[dict, dict]: |
| 343 | items_schema = property_schema.get("items") |
| 344 | if not isinstance(items_schema, dict): |
| 345 | return property_schema, {} |
| 346 | aliased_items, aliases = _alias_schema_properties(items_schema) |
| 347 | if aliases: |
| 348 | property_schema["items"] = aliased_items |
| 349 | return property_schema, aliases |
| 350 | |
| 351 | |
| 352 | def restore_tool_arguments(request: dict, aliases: dict) -> dict: |
| 353 | """Restore provider-visible argument aliases back to backend schema names. |
| 354 | |
| 355 | Modifies *request* in-place and returns it. |
| 356 | """ |
| 357 | alias_keys = [ |
| 358 | key |
| 359 | for key in aliases |
| 360 | if not key.endswith(_OBJ_MARKER) and not key.endswith(_ARR_MARKER) |
| 361 | ] |
| 362 | for clean_key in sorted(alias_keys, reverse=True): |
| 363 | if clean_key not in request: |
| 364 | continue |
| 365 | |
| 366 | original_value = request.pop(clean_key) |
| 367 | object_aliases = aliases.get(f"{clean_key}{_OBJ_MARKER}", {}) |
| 368 | array_aliases = aliases.get(f"{clean_key}{_ARR_MARKER}", {}) |
| 369 | if object_aliases and isinstance(original_value, dict): |
| 370 | original_value = restore_tool_arguments( |
| 371 | request=original_value, |
| 372 | aliases=object_aliases, |
| 373 | ) |
| 374 | if array_aliases and isinstance(original_value, list): |
| 375 | original_value = [ |
| 376 | restore_tool_arguments(item, array_aliases) |
| 377 | if isinstance(item, dict) |
| 378 | else item |
| 379 | for item in original_value |
| 380 | ] |
| 381 | request[aliases.get(clean_key, clean_key)] = original_value |
| 382 | return request |
| 383 | |
| 384 | |
| 385 | def substitute_reserved_python_keywords( |
| 386 | schema: t.Dict, |
| 387 | ) -> t.Tuple[dict, dict]: |
| 388 | """Replace unsafe JSON schema property names with Python parameter aliases. |
| 389 | |
| 390 | Backward-compatible wrapper around :func:`alias_tool_input_schema`. |
| 391 | """ |
| 392 | aliased = alias_tool_input_schema(schema=schema) |
| 393 | return aliased.schema, aliased.aliases |
| 394 | |
| 395 | |
| 396 | def reinstate_reserved_python_keywords( |
| 397 | request: dict, |
| 398 | keywords: dict, |
| 399 | ) -> dict: |
| 400 | """Reverse the substitution performed by :func:`substitute_reserved_python_keywords`. |
| 401 | |
| 402 | Modifies *request* **in-place** and returns it. |
| 403 | """ |
| 404 | return restore_tool_arguments(request=request, aliases=keywords) |
| 405 | |
| 406 | |
| 407 | def _coerce_default_value( |
| 408 | default: t.Any, |
| 409 | json_schema: t.Dict[str, t.Any], |
| 410 | ) -> t.Any: |
| 411 | """ |
| 412 | Coerce a default value to match the expected type from JSON schema. |
| 413 | |
| 414 | Handles common mismatches where string defaults should be boolean/int/float. |
| 415 | This fixes issues where API returns stringified defaults like "true" instead of true. |
| 416 | |
| 417 | Coercion precedence: boolean > integer > float. This means values like "1" and "0" |
| 418 | become booleans when both bool and int are expected types. |
| 419 | |
| 420 | :param default: The default value from the JSON schema. |
| 421 | :param json_schema: The JSON schema property definition. |
| 422 | :return: The coerced default value, or original if no coercion possible. |
| 423 | """ |
| 424 | if default is None or not isinstance(default, str): |
| 425 | return default |
| 426 | |
| 427 | # Collect expected types from schema |
| 428 | expected_types: t.Set[t.Any] = set() |
| 429 | |
| 430 | def add_types(schema_type: t.Any) -> None: |
| 431 | # `type` may be a single name or a list of names (e.g. ["string", "null"]). |
| 432 | names = schema_type if isinstance(schema_type, list) else [schema_type] |
| 433 | for type_name in names: |
| 434 | if isinstance(type_name, str): |
| 435 | py_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_name) |
| 436 | if py_type is not None: |
| 437 | expected_types.add(py_type) |
| 438 | |
| 439 | add_types(json_schema.get("type")) |
| 440 | |
| 441 | for combiner in ("anyOf", "oneOf", "allOf"): |
| 442 | for option in json_schema.get(combiner, []): |
| 443 | if isinstance(option, dict): |
| 444 | add_types(option.get("type")) |
| 445 | |
| 446 | # If string is expected, no coercion needed |
| 447 | if str in expected_types: |
| 448 | return default |
| 449 | |
| 450 | # Boolean coercion (takes precedence over int for "1"/"0") |
| 451 | if bool in expected_types: |
| 452 | lower_default = default.lower() |
| 453 | if lower_default in ("true", "yes", "1"): |
| 454 | return True |
| 455 | if lower_default in ("false", "no", "0"): |
| 456 | return False |
| 457 | |
| 458 | # Integer coercion |
| 459 | if int in expected_types: |
| 460 | try: |
| 461 | return int(default) |
| 462 | except ValueError: |
| 463 | pass |
| 464 | |
| 465 | # Float coercion |
| 466 | if float in expected_types: |
| 467 | try: |
| 468 | return float(default) |
| 469 | except ValueError: |
| 470 | pass |
| 471 | |
| 472 | return default |
| 473 | |
| 474 | |
| 475 | def json_schema_to_pydantic_field( |
| 476 | name: str, |
| 477 | json_schema: t.Union[t.Dict[str, t.Any], bool], |
| 478 | required: t.List[str], |
| 479 | skip_default: bool = False, |
| 480 | *, |
| 481 | root_schema: t.Optional[t.Dict[str, t.Any]] = None, |
| 482 | ) -> t.Tuple[str, t.Type, FieldInfo]: |
| 483 | """ |
| 484 | Converts a JSON schema property to a Pydantic field definition. |
| 485 | |
| 486 | :param name: The field name. |
| 487 | :param json_schema: The JSON schema property. |
| 488 | :param required: List of required properties. |
| 489 | :param root_schema: Full schema document used to resolve local references. |
| 490 | :return: A Pydantic field definition. |
| 491 | """ |
| 492 | # A property schema may be the literal `true`/`false` (JSON Schema |
| 493 | # draft-06+); those carry no metadata, so read annotations from an empty |
| 494 | # object and let `json_schema_to_pydantic_type` decide the type. |
| 495 | schema_object = json_schema if isinstance(json_schema, dict) else {} |
| 496 | description = schema_object.get("description") |
| 497 | if "oneOf" in schema_object: |
| 498 | description = " | ".join( |
| 499 | [option.get("description", "") for option in schema_object["oneOf"]] |
| 500 | ) |
| 501 | description = f"Any of the following options(separated by |): {description}" |
| 502 | |
| 503 | examples = schema_object.get("examples", []) |
| 504 | default = schema_object.get("default") |
| 505 | |
| 506 | # Coerce default value to match expected type from schema |
| 507 | if default is not None: |
| 508 | default = _coerce_default_value(default, schema_object) |
| 509 | |
| 510 | # Check if the field name is a reserved Pydantic name |
| 511 | original_name = name |
| 512 | if name in reserved_names: |
| 513 | name = f"{name}_" |
| 514 | alias = original_name |
| 515 | else: |
| 516 | alias = None |
| 517 | |
| 518 | field = { |
| 519 | "description": description, |
| 520 | "examples": examples, |
| 521 | "alias": alias, |
| 522 | } |
| 523 | if not skip_default: |
| 524 | field["default"] = ... if original_name in required else default |
| 525 | |
| 526 | return ( |
| 527 | name, |
| 528 | t.cast( |
| 529 | t.Type, |
| 530 | json_schema_to_pydantic_type( |
| 531 | json_schema=json_schema, |
| 532 | root_schema=root_schema, |
| 533 | ), |
| 534 | ), |
| 535 | Field(**field), # type: ignore |
| 536 | ) |
| 537 | |
| 538 | |
| 539 | def json_schema_to_fields_dict(json_schema: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: |
| 540 | """ |
| 541 | Converts a JSON schema to a dictionary of param name, and a tuple of type & Field. |
| 542 | |
| 543 | :param json_schema: The JSON schema to convert. |
| 544 | :return: dict<str, tuple<<class 'type'>, Field>> |
| 545 | |
| 546 | Example Output: |
| 547 | ```python |
| 548 | { |
| 549 | 'owner': (<class 'str'>, FieldInfo(default=Ellipsis, description='The account owner of the repository.', extra={'examples': ([],)})), |
| 550 | 'repo': (<class 'str'>, FieldInfo(default=Ellipsis, description='The name of the repository without the `.git` extension.', extra={'examples': ([],)}))} |
| 551 | } |
| 552 | ``` |
| 553 | |
| 554 | """ |
| 555 | field_definitions = {} |
| 556 | for name, prop in json_schema.get("properties", {}).items(): |
| 557 | updated_name, pydantic_type, pydantic_field = json_schema_to_pydantic_field( |
| 558 | name, |
| 559 | prop, |
| 560 | json_schema.get("required", []), |
| 561 | root_schema=json_schema, |
| 562 | ) |
| 563 | field_definitions[updated_name] = (pydantic_type, pydantic_field) |
| 564 | return field_definitions # type: ignore |
| 565 | |
| 566 | |
| 567 | def json_schema_to_model( |
| 568 | json_schema: t.Dict[str, t.Any], |
| 569 | skip_default: bool = False, |
| 570 | ) -> t.Type[BaseModel]: |
| 571 | """ |
| 572 | Converts a JSON schema to a Pydantic BaseModel class. |
| 573 | |
| 574 | :param json_schema: The JSON schema to convert. |
| 575 | :param skip_default: Skip the default values when building field object |
| 576 | :return: Pydantic `BaseModel` type |
| 577 | """ |
| 578 | model_name = json_schema.get("title") |
| 579 | if model_name is None: |
| 580 | model_name = "GeneratedModel" |
| 581 | field_definitions = {} |
| 582 | for name, prop in json_schema.get("properties", {}).items(): |
| 583 | updated_name, pydantic_type, pydantic_field = json_schema_to_pydantic_field( |
| 584 | name, |
| 585 | prop, |
| 586 | json_schema.get("required", []), |
| 587 | skip_default=skip_default, |
| 588 | root_schema=json_schema, |
| 589 | ) |
| 590 | field_definitions[updated_name] = (pydantic_type, pydantic_field) |
| 591 | # The dynamic-key policy is shared with `json_schema_to_pydantic_type` so the |
| 592 | # two entry points cannot disagree about which arguments survive conversion. |
| 593 | base_model = create_model(model_name, **field_definitions) # type: ignore |
| 594 | if skip_default: |
| 595 | setattr(base_model, _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, frozenset()) |
| 596 | else: |
| 597 | _mark_explicit_default_fields(base_model, json_schema, json_schema) |
| 598 | # Exact acceptance is shared with `json_schema_to_pydantic_type` so every |
| 599 | # Python entry point agrees with the Zod and Effect converters about which |
| 600 | # inputs a schema admits. |
| 601 | return _with_exact_validation( |
| 602 | apply_object_policy( |
| 603 | json_schema, |
| 604 | base_model, |
| 605 | model_name=model_name, |
| 606 | ), |
| 607 | json_schema, |
| 608 | json_schema, |
| 609 | ) |
| 610 | |
| 611 | |
| 612 | def pydantic_model_from_param_schema(param_schema: t.Dict) -> t.Type: |
| 613 | """ |
| 614 | Dynamically creates a Pydantic model from a schema dictionary. |
| 615 | |
| 616 | :param param_schema: Schema with 'title', 'properties', and optionally 'required' keys. |
| 617 | :return: A Pydantic model class for the defined schema. |
| 618 | |
| 619 | :raised ValueError: Invalid 'type' for property or recursive model creation. |
| 620 | |
| 621 | Note: Requires global `schema_type_python_type_dict` for type mapping and |
| 622 | `fallback_values` for default values. |
| 623 | """ |
| 624 | required_fields = {} |
| 625 | optional_fields = {} |
| 626 | if "title" not in param_schema: |
| 627 | raise ValueError(f"Missing 'title' in param_schema: {param_schema}") |
| 628 | |
| 629 | param_title = str(param_schema["title"]).replace(" ", "") |
| 630 | required_props = param_schema.get("required", []) |
| 631 | |
| 632 | if param_schema.get("type") == "array": |
| 633 | return t.cast(t.Type, json_schema_to_pydantic_type(param_schema)) |
| 634 | |
| 635 | for prop_name, prop_info in param_schema.get("properties", {}).items(): |
| 636 | prop_object = prop_info if isinstance(prop_info, dict) else {} |
| 637 | prop_type = prop_object.get("type") |
| 638 | prop_title = prop_object.get("title", prop_name).replace(" ", "") |
| 639 | fallback = ( |
| 640 | FALLBACK_VALUES.get(prop_type) if isinstance(prop_type, str) else None |
| 641 | ) |
| 642 | prop_default = prop_object.get("default", fallback) |
| 643 | signature_prop_type = t.cast( |
| 644 | t.Type, |
| 645 | json_schema_to_pydantic_type( |
| 646 | json_schema=prop_info, |
| 647 | root_schema=param_schema, |
| 648 | ), |
| 649 | ) |
| 650 | |
| 651 | field_kwargs = { |
| 652 | "description": prop_object.get( |
| 653 | "description", prop_object.get("desc", prop_title) |
| 654 | ), |
| 655 | } |
| 656 | |
| 657 | # Add alias if the field name is a reserved Pydantic name |
| 658 | if prop_name in reserved_names: |
| 659 | field_kwargs["alias"] = prop_name |
| 660 | field_kwargs["title"] = f"{prop_name}_" |
| 661 | else: |
| 662 | field_kwargs["title"] = prop_title |
| 663 | |
| 664 | if prop_name in required_props: |
| 665 | required_fields[prop_name] = ( |
| 666 | signature_prop_type, |
| 667 | Field(..., **field_kwargs), |
| 668 | ) |
| 669 | else: |
| 670 | optional_fields[prop_name] = ( |
| 671 | signature_prop_type, |
| 672 | Field(default=prop_default, **field_kwargs), |
| 673 | ) |
| 674 | |
| 675 | if not required_fields and not optional_fields: |
| 676 | # Nothing to materialize: let the shared converter pick the annotation |
| 677 | # so `allOf`/`anyOf`/`$ref` and typeless assertions keep the same exact |
| 678 | # validation and permissive materialization as the other entry points. |
| 679 | return t.cast(t.Type, json_schema_to_pydantic_type(param_schema)) |
| 680 | |
| 681 | model = create_model( # type: ignore |
| 682 | param_title, |
| 683 | **required_fields, |
| 684 | **optional_fields, |
| 685 | ) |
| 686 | return _with_exact_validation(model, param_schema, param_schema) |
| 687 | |
| 688 | |
| 689 | def get_signature_format_from_schema_params( |
| 690 | schema_params: t.Dict, |
| 691 | skip_default: bool = False, |
| 692 | ) -> t.List[Parameter]: |
| 693 | """ |
| 694 | Get function parameters signature(with pydantic field definition as default values) |
| 695 | from schema parameters. Works like: |
| 696 | |
| 697 | def demo_function( |
| 698 | owner: str, |
| 699 | repo: str), |
| 700 | ) |
| 701 | |
| 702 | :param schema_params: A dictionary object containing schema params, with keys [properties, required etc.]. |
| 703 | :return: List of required and optional parameters |
| 704 | |
| 705 | Output Format: |
| 706 | [ |
| 707 | <Parameter "owner: str">, |
| 708 | <Parameter "repo: str"> |
| 709 | ] |
| 710 | """ |
| 711 | default_parameters = [] |
| 712 | none_default_parameters = [] |
| 713 | |
| 714 | required_params = schema_params.get("required", []) |
| 715 | schema_params_object = schema_params.get("properties", {}) |
| 716 | for param_name, param_schema in schema_params_object.items(): |
| 717 | param_type = param_schema.get("type", None) |
| 718 | param_oneOf = param_schema.get("oneOf", None) |
| 719 | param_anyOf = param_schema.get("anyOf", None) |
| 720 | param_allOf = param_schema.get("allOf", None) |
| 721 | if param_allOf is not None and len(param_allOf) == 1: |
| 722 | param_type = param_allOf[0].get("type", None) |
| 723 | if param_oneOf is not None or param_anyOf is not None: |
| 724 | param_types = [ptype.get("type") for ptype in (param_oneOf or param_anyOf)] |
| 725 | # Map each option to a Python type, falling back to t.Any for options |
| 726 | # that are missing a "type" key or use an unrecognized type, then build |
| 727 | # a Union for any count of members (no 1/2/3-member cap). |
| 728 | mapped_types: t.List[t.Any] = [ |
| 729 | _annotation_from_json_schema_type(ptype) for ptype in param_types |
| 730 | ] |
| 731 | if len(mapped_types) == 1: |
| 732 | annotation = mapped_types[0] |
| 733 | else: |
| 734 | annotation = reduce(lambda a, b: t.Union[a, b], mapped_types) |
| 735 | param_default = param_schema.get("default", "") |
| 736 | elif isinstance(param_type, list): |
| 737 | annotation = _annotation_from_json_schema_type(param_type) |
| 738 | scalar_type = param_type[0] if len(param_type) == 1 else None |
| 739 | if isinstance(scalar_type, str) and scalar_type in FALLBACK_VALUES: |
| 740 | param_default = param_schema.get( |
| 741 | "default", FALLBACK_VALUES[scalar_type] |
| 742 | ) |
| 743 | else: |
| 744 | param_default = param_schema.get("default", "") |
| 745 | elif param_type in PYDANTIC_TYPE_TO_PYTHON_TYPE: |
| 746 | annotation = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_type] |
| 747 | param_default = param_schema.get("default", FALLBACK_VALUES[param_type]) |
| 748 | else: |
| 749 | annotation = pydantic_model_from_param_schema(param_schema) |
| 750 | if param_type is None or param_type == "null": |
| 751 | param_default = None |
| 752 | else: |
| 753 | param_default = param_schema.get("default", FALLBACK_VALUES[param_type]) |
| 754 | |
| 755 | default = param_default |
| 756 | required = param_schema.get("required", False) or param_name in required_params |
| 757 | if required: |
| 758 | default = Parameter.empty |
| 759 | |
| 760 | if skip_default: |
| 761 | default = Parameter.empty |
| 762 | |
| 763 | parameter = Parameter( |
| 764 | name=param_name, |
| 765 | kind=Parameter.POSITIONAL_OR_KEYWORD, |
| 766 | annotation=annotation, |
| 767 | default=default, |
| 768 | ) |
| 769 | if required: |
| 770 | default_parameters.append(parameter) |
| 771 | continue |
| 772 | none_default_parameters.append(parameter) |
| 773 | return default_parameters + none_default_parameters |
| 774 | |
| 775 | |
| 776 | def _annotation_from_json_schema_type(schema_type: t.Any) -> t.Any: |
| 777 | """Convert a JSON Schema ``type`` value to a Python annotation. |
| 778 | |
| 779 | JSON Schema Draft 2020-12 and OpenAPI 3.1 allow ``type`` to be an array, |
| 780 | including inside ``anyOf`` and ``oneOf`` branches. Keep that form from |
| 781 | reaching the scalar dictionary lookup, where a list would be unhashable. |
| 782 | """ |
| 783 | if isinstance(schema_type, list): |
| 784 | mapped_types = [_annotation_from_json_schema_type(item) for item in schema_type] |
| 785 | if not mapped_types: |
| 786 | return t.Any |
| 787 | if len(mapped_types) == 1: |
| 788 | return mapped_types[0] |
| 789 | return reduce(lambda left, right: t.Union[left, right], mapped_types) |
| 790 | |
| 791 | if isinstance(schema_type, str): |
| 792 | return PYDANTIC_TYPE_TO_PYTHON_TYPE.get(schema_type, t.Any) |
| 793 | |
| 794 | return t.Any |
| 795 | |
| 796 | |
| 797 | def get_pydantic_signature_format_from_schema_params( |
| 798 | schema_params: t.Dict, |
| 799 | skip_default: bool = False, |
| 800 | ) -> t.List[Parameter]: |
| 801 | """ |
| 802 | Get function parameters signature(with pydantic field definition as default values) |
| 803 | from schema parameters. Works like: |
| 804 | |
| 805 | def demo_function( |
| 806 | owner: str=Field(..., description='The account owner of the repository.'), |
| 807 | repo: str=Field(..., description='The name of the repository without the `.git` extension.'), |
| 808 | ) |
| 809 | |
| 810 | :param schema_params: A dictionary object containing schema params, with keys [properties, required etc.]. |
| 811 | :return: List of required and optional parameters |
| 812 | |
| 813 | Example Output Format: |
| 814 | ```python |
| 815 | [ |
| 816 | <Parameter "owner: str = FieldInfo( |
| 817 | default=Ellipsis, |
| 818 | description='The account owner of the repository.', |
| 819 | extra={'examples': ([],)})">, |
| 820 | <Parameter "repo: str = FieldInfo( |
| 821 | default=Ellipsis, |
| 822 | description='The name of the repository without the `.git` extension.', |
| 823 | extra={'examples': ([],)})"> |
| 824 | ] |
| 825 | ``` |
| 826 | """ |
| 827 | all_parameters = [] |
| 828 | field_definitions = json_schema_to_fields_dict(schema_params) |
| 829 | for param_name, (param_dtype, parame_field) in field_definitions.items(): |
| 830 | param = Parameter( |
| 831 | name=param_name, |
| 832 | kind=Parameter.POSITIONAL_OR_KEYWORD, |
| 833 | annotation=param_dtype, |
| 834 | default=Parameter.empty if skip_default else parame_field.default, |
| 835 | ) |
| 836 | all_parameters.append(param) |
| 837 | |
| 838 | return all_parameters |
| 839 | |
| 840 | |
| 841 | def generate_request_id() -> str: |
| 842 | """Generate a unique request ID.""" |
| 843 | return str(uuid.uuid4()) |