Skip to content

Commit 80b82c0

Browse files
committed
fix(responses): type aliasing not supported for pydantic code generation and discrimintated unions
1 parent 8fb17ba commit 80b82c0

File tree

5 files changed

+287
-73
lines changed

5 files changed

+287
-73
lines changed

docs/_static/llama-stack-spec.html

Lines changed: 156 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8993,9 +8993,163 @@
89938993
"title": "OpenAIResponsesTool"
89948994
},
89958995
"OpenAIResponsesToolChoice": {
8996+
"oneOf": [
8997+
{
8998+
"$ref": "#/components/schemas/ToolChoiceOptions"
8999+
},
9000+
{
9001+
"$ref": "#/components/schemas/ToolChoiceTypes"
9002+
},
9003+
{
9004+
"oneOf": [
9005+
{
9006+
"$ref": "#/components/schemas/ToolChoiceAllowed"
9007+
},
9008+
{
9009+
"$ref": "#/components/schemas/ToolChoiceFunction"
9010+
},
9011+
{
9012+
"$ref": "#/components/schemas/ToolChoiceMcp"
9013+
},
9014+
{
9015+
"$ref": "#/components/schemas/ToolChoiceCustom"
9016+
}
9017+
],
9018+
"discriminator": {
9019+
"propertyName": "type",
9020+
"mapping": {
9021+
"allowed_tools": "#/components/schemas/ToolChoiceAllowed",
9022+
"function": "#/components/schemas/ToolChoiceFunction",
9023+
"mcp": "#/components/schemas/ToolChoiceMcp",
9024+
"custom": "#/components/schemas/ToolChoiceCustom"
9025+
}
9026+
}
9027+
}
9028+
]
9029+
},
9030+
"ToolChoiceAllowed": {
9031+
"type": "object",
9032+
"properties": {
9033+
"mode": {
9034+
"type": "string",
9035+
"enum": [
9036+
"auto",
9037+
"required"
9038+
]
9039+
},
9040+
"tools": {
9041+
"type": "array",
9042+
"items": {
9043+
"type": "object",
9044+
"additionalProperties": {
9045+
"type": "object",
9046+
"title": "object",
9047+
"description": "The base class of the class hierarchy.\nWhen called, it accepts no arguments and returns a new featureless\ninstance that has no instance attributes and cannot be given any."
9048+
}
9049+
}
9050+
},
9051+
"type": {
9052+
"type": "string",
9053+
"const": "allowed_tools",
9054+
"default": "allowed_tools"
9055+
}
9056+
},
9057+
"additionalProperties": false,
9058+
"required": [
9059+
"mode",
9060+
"tools",
9061+
"type"
9062+
],
9063+
"title": "ToolChoiceAllowed"
9064+
},
9065+
"ToolChoiceCustom": {
89969066
"type": "object",
8997-
"title": "OpenAIResponsesToolChoice",
8998-
"description": "Type alias.\nType aliases are created through the type statement::\n\n type Alias = int\n\nIn this example, Alias and int will be treated equivalently by static\ntype checkers.\n\nAt runtime, Alias is an instance of TypeAliasType. The __name__\nattribute holds the name of the type alias. The value of the type alias\nis stored in the __value__ attribute. It is evaluated lazily, so the\nvalue is computed only if the attribute is accessed.\n\nType aliases can also be generic::\n\n type ListOrSet[T] = list[T] | set[T]\n\nIn this case, the type parameters of the alias are stored in the\n__type_params__ attribute.\n\nSee PEP 695 for more information."
9067+
"properties": {
9068+
"name": {
9069+
"type": "string"
9070+
},
9071+
"type": {
9072+
"type": "string",
9073+
"const": "custom",
9074+
"default": "custom"
9075+
}
9076+
},
9077+
"additionalProperties": false,
9078+
"required": [
9079+
"name",
9080+
"type"
9081+
],
9082+
"title": "ToolChoiceCustom"
9083+
},
9084+
"ToolChoiceFunction": {
9085+
"type": "object",
9086+
"properties": {
9087+
"name": {
9088+
"type": "string"
9089+
},
9090+
"type": {
9091+
"type": "string",
9092+
"const": "function",
9093+
"default": "function"
9094+
}
9095+
},
9096+
"additionalProperties": false,
9097+
"required": [
9098+
"name",
9099+
"type"
9100+
],
9101+
"title": "ToolChoiceFunction"
9102+
},
9103+
"ToolChoiceMcp": {
9104+
"type": "object",
9105+
"properties": {
9106+
"server_label": {
9107+
"type": "string"
9108+
},
9109+
"type": {
9110+
"type": "string",
9111+
"const": "mcp",
9112+
"default": "mcp"
9113+
},
9114+
"name": {
9115+
"type": "string"
9116+
}
9117+
},
9118+
"additionalProperties": false,
9119+
"required": [
9120+
"server_label",
9121+
"type"
9122+
],
9123+
"title": "ToolChoiceMcp"
9124+
},
9125+
"ToolChoiceOptions": {
9126+
"type": "string",
9127+
"enum": [
9128+
"none",
9129+
"auto",
9130+
"required"
9131+
]
9132+
},
9133+
"ToolChoiceTypes": {
9134+
"type": "object",
9135+
"properties": {
9136+
"type": {
9137+
"type": "string",
9138+
"enum": [
9139+
"file_search",
9140+
"web_search_preview",
9141+
"computer_use_preview",
9142+
"web_search_preview_2025_03_11",
9143+
"image_generation",
9144+
"code_interpreter"
9145+
]
9146+
}
9147+
},
9148+
"additionalProperties": false,
9149+
"required": [
9150+
"type"
9151+
],
9152+
"title": "ToolChoiceTypes"
89999153
},
90009154
"OpenAIResponseContentPart": {
90019155
"oneOf": [

docs/_static/llama-stack-spec.yaml

Lines changed: 109 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6589,39 +6589,118 @@ components:
65896589
additionalProperties: false
65906590
title: OpenAIResponsesTool
65916591
OpenAIResponsesToolChoice:
6592+
oneOf:
6593+
- $ref: '#/components/schemas/ToolChoiceOptions'
6594+
- $ref: '#/components/schemas/ToolChoiceTypes'
6595+
- oneOf:
6596+
- $ref: '#/components/schemas/ToolChoiceAllowed'
6597+
- $ref: '#/components/schemas/ToolChoiceFunction'
6598+
- $ref: '#/components/schemas/ToolChoiceMcp'
6599+
- $ref: '#/components/schemas/ToolChoiceCustom'
6600+
discriminator:
6601+
propertyName: type
6602+
mapping:
6603+
allowed_tools: '#/components/schemas/ToolChoiceAllowed'
6604+
function: '#/components/schemas/ToolChoiceFunction'
6605+
mcp: '#/components/schemas/ToolChoiceMcp'
6606+
custom: '#/components/schemas/ToolChoiceCustom'
6607+
ToolChoiceAllowed:
65926608
type: object
6593-
title: OpenAIResponsesToolChoice
6594-
description: >-
6595-
Type alias.
6596-
6597-
Type aliases are created through the type statement::
6598-
6599-
type Alias = int
6600-
6601-
In this example, Alias and int will be treated equivalently by static
6602-
6603-
type checkers.
6604-
6605-
6606-
At runtime, Alias is an instance of TypeAliasType. The __name__
6607-
6608-
attribute holds the name of the type alias. The value of the type alias
6609-
6610-
is stored in the __value__ attribute. It is evaluated lazily, so the
6611-
6612-
value is computed only if the attribute is accessed.
6613-
6614-
6615-
Type aliases can also be generic::
6616-
6617-
type ListOrSet[T] = list[T] | set[T]
6618-
6619-
In this case, the type parameters of the alias are stored in the
6620-
6621-
__type_params__ attribute.
6609+
properties:
6610+
mode:
6611+
type: string
6612+
enum:
6613+
- auto
6614+
- required
6615+
tools:
6616+
type: array
6617+
items:
6618+
type: object
6619+
additionalProperties:
6620+
type: object
6621+
title: object
6622+
description: >-
6623+
The base class of the class hierarchy.
66226624
6625+
When called, it accepts no arguments and returns a new featureless
66236626
6624-
See PEP 695 for more information.
6627+
instance that has no instance attributes and cannot be given any.
6628+
type:
6629+
type: string
6630+
const: allowed_tools
6631+
default: allowed_tools
6632+
additionalProperties: false
6633+
required:
6634+
- mode
6635+
- tools
6636+
- type
6637+
title: ToolChoiceAllowed
6638+
ToolChoiceCustom:
6639+
type: object
6640+
properties:
6641+
name:
6642+
type: string
6643+
type:
6644+
type: string
6645+
const: custom
6646+
default: custom
6647+
additionalProperties: false
6648+
required:
6649+
- name
6650+
- type
6651+
title: ToolChoiceCustom
6652+
ToolChoiceFunction:
6653+
type: object
6654+
properties:
6655+
name:
6656+
type: string
6657+
type:
6658+
type: string
6659+
const: function
6660+
default: function
6661+
additionalProperties: false
6662+
required:
6663+
- name
6664+
- type
6665+
title: ToolChoiceFunction
6666+
ToolChoiceMcp:
6667+
type: object
6668+
properties:
6669+
server_label:
6670+
type: string
6671+
type:
6672+
type: string
6673+
const: mcp
6674+
default: mcp
6675+
name:
6676+
type: string
6677+
additionalProperties: false
6678+
required:
6679+
- server_label
6680+
- type
6681+
title: ToolChoiceMcp
6682+
ToolChoiceOptions:
6683+
type: string
6684+
enum:
6685+
- none
6686+
- auto
6687+
- required
6688+
ToolChoiceTypes:
6689+
type: object
6690+
properties:
6691+
type:
6692+
type: string
6693+
enum:
6694+
- file_search
6695+
- web_search_preview
6696+
- computer_use_preview
6697+
- web_search_preview_2025_03_11
6698+
- image_generation
6699+
- code_interpreter
6700+
additionalProperties: false
6701+
required:
6702+
- type
6703+
title: ToolChoiceTypes
66256704
OpenAIResponseContentPart:
66266705
oneOf:
66276706
- $ref: '#/components/schemas/OpenAIResponseContentPartOutputText'

llama_stack/apis/agents/openai_responses.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This source code is licensed under the terms described in the LICENSE file in
55
# the root directory of this source tree.
66

7-
from typing import Annotated, Any, Literal, Union
7+
from typing import Annotated, Any, Literal
88

99
from pydantic import BaseModel, Field
1010
from typing_extensions import TypedDict
@@ -14,21 +14,20 @@
1414
ToolChoiceCustom,
1515
ToolChoiceFunction,
1616
ToolChoiceMcp,
17+
ToolChoiceOptions,
1718
ToolChoiceTypes,
1819
)
1920
from llama_stack.apis.vector_io import SearchRankingOptions as FileSearchRankingOptions
2021
from llama_stack.schema_utils import json_schema_type, register_schema
2122

22-
type OpenAIResponsesToolChoice = Annotated[
23-
Union[
24-
ToolChoiceTypes,
25-
ToolChoiceAllowed,
26-
ToolChoiceFunction,
27-
ToolChoiceMcp,
28-
ToolChoiceCustom
29-
],
30-
Field(discriminator="type"),
31-
]
23+
OpenAIResponsesToolChoice = (
24+
ToolChoiceOptions
25+
| ToolChoiceTypes # Multiple type values - can't use a discriminator here
26+
| Annotated[
27+
ToolChoiceAllowed | ToolChoiceFunction | ToolChoiceMcp | ToolChoiceCustom,
28+
Field(discriminator="type"),
29+
]
30+
)
3231
register_schema(OpenAIResponsesToolChoice, name="OpenAIResponsesToolChoice")
3332

3433

0 commit comments

Comments
 (0)