Add Form() annotations and fix OpenAPI generation for multipart params

- Add Form() annotations to purpose and expires_after parameters in file upload endpoints
- Add support for optional multipart form parameters in OpenAPI generator
- Generated spec now properly mirrors OpenAI format with schema refs
This commit is contained in:
Ashwin Bharambe 2025-09-29 20:14:39 -07:00
parent 8288122146
commit f676c48a97
8 changed files with 705 additions and 362 deletions

View file

@ -5,6 +5,7 @@
# the root directory of this source tree.
import hashlib
import inspect
import ipaddress
import types
import typing
@ -12,6 +13,7 @@ from dataclasses import make_dataclass
from typing import Annotated, Any, Dict, get_args, get_origin, Set, Union
from fastapi import UploadFile
from pydantic import BaseModel
from llama_stack.apis.datatypes import Error
from llama_stack.strong_typing.core import JsonType
@ -632,14 +634,22 @@ class Generator:
base_type = get_args(param_type)[0]
else:
base_type = param_type
# Check if the type is optional
is_optional = is_type_optional(base_type)
if is_optional:
base_type = unwrap_optional_type(base_type)
if base_type is UploadFile:
# File upload
properties[name] = {"type": "string", "format": "binary"}
else:
# Form field
# All other types - generate schema reference
# This includes enums, BaseModels, and simple types
properties[name] = self.schema_builder.classdef_to_ref(base_type)
required_fields.append(name)
if not is_optional:
required_fields.append(name)
multipart_schema = {
"type": "object",