From 9f96aca4d27c52722feb856b8d644a8791e21ec6 Mon Sep 17 00:00:00 2001 From: Nathan Weinberg Date: Tue, 22 Jul 2025 16:08:59 -0400 Subject: [PATCH] feat: add file support for OpenAI Chat Completion requests Signed-off-by: Nathan Weinberg --- docs/_static/llama-stack-spec.html | 41 ++++++++++++++++++++++++- docs/_static/llama-stack-spec.yaml | 27 ++++++++++++++++ llama_stack/apis/inference/inference.py | 15 ++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 38e53a438..e77995aaa 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -9821,13 +9821,17 @@ }, { "$ref": "#/components/schemas/OpenAIChatCompletionContentPartImageParam" + }, + { + "$ref": "#/components/schemas/OpenAIFile" } ], "discriminator": { "propertyName": "type", "mapping": { "text": "#/components/schemas/OpenAIChatCompletionContentPartTextParam", - "image_url": "#/components/schemas/OpenAIChatCompletionContentPartImageParam" + "image_url": "#/components/schemas/OpenAIChatCompletionContentPartImageParam", + "file": "#/components/schemas/OpenAIFile" } } }, @@ -9974,6 +9978,41 @@ "title": "OpenAIDeveloperMessageParam", "description": "A message from the developer in an OpenAI-compatible chat completion request." }, + "OpenAIFile": { + "type": "object", + "properties": { + "type": { + "type": "string", + "const": "file", + "default": "file" + }, + "file": { + "$ref": "#/components/schemas/OpenAIFileFile" + } + }, + "additionalProperties": false, + "required": [ + "type", + "file" + ], + "title": "OpenAIFile" + }, + "OpenAIFileFile": { + "type": "object", + "properties": { + "file_data": { + "type": "string" + }, + "file_id": { + "type": "string" + }, + "filename": { + "type": "string" + } + }, + "additionalProperties": false, + "title": "OpenAIFileFile" + }, "OpenAIImageURL": { "type": "object", "properties": { diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index 0df60ddf4..55b2dddca 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -6934,11 +6934,13 @@ components: oneOf: - $ref: '#/components/schemas/OpenAIChatCompletionContentPartTextParam' - $ref: '#/components/schemas/OpenAIChatCompletionContentPartImageParam' + - $ref: '#/components/schemas/OpenAIFile' discriminator: propertyName: type mapping: text: '#/components/schemas/OpenAIChatCompletionContentPartTextParam' image_url: '#/components/schemas/OpenAIChatCompletionContentPartImageParam' + file: '#/components/schemas/OpenAIFile' OpenAIChatCompletionContentPartTextParam: type: object properties: @@ -7050,6 +7052,31 @@ components: title: OpenAIDeveloperMessageParam description: >- A message from the developer in an OpenAI-compatible chat completion request. + OpenAIFile: + type: object + properties: + type: + type: string + const: file + default: file + file: + $ref: '#/components/schemas/OpenAIFileFile' + additionalProperties: false + required: + - type + - file + title: OpenAIFile + OpenAIFileFile: + type: object + properties: + file_data: + type: string + file_id: + type: string + filename: + type: string + additionalProperties: false + title: OpenAIFileFile OpenAIImageURL: type: object properties: diff --git a/llama_stack/apis/inference/inference.py b/llama_stack/apis/inference/inference.py index 222099064..6be418d8c 100644 --- a/llama_stack/apis/inference/inference.py +++ b/llama_stack/apis/inference/inference.py @@ -455,8 +455,21 @@ class OpenAIChatCompletionContentPartImageParam(BaseModel): image_url: OpenAIImageURL +@json_schema_type +class OpenAIFileFile(BaseModel): + file_data: str | None = None + file_id: str | None = None + filename: str | None = None + + +@json_schema_type +class OpenAIFile(BaseModel): + type: Literal["file"] = "file" + file: OpenAIFileFile + + OpenAIChatCompletionContentPartParam = Annotated[ - OpenAIChatCompletionContentPartTextParam | OpenAIChatCompletionContentPartImageParam, + OpenAIChatCompletionContentPartTextParam | OpenAIChatCompletionContentPartImageParam | OpenAIFile, Field(discriminator="type"), ] register_schema(OpenAIChatCompletionContentPartParam, name="OpenAIChatCompletionContentPartParam")