Skip to main content

async_openai/types/chat/
schema.rs

1//! Schema-only implementations for unions not represented faithfully by utoipa's derive.
2//!
3//! Keep Serde on the public types unchanged. In particular, per-variant
4//! `serde(untagged)` must not introduce a wrapper named after the Rust variant.
5
6use super::*;
7use crate::types::shared::schema::schema;
8use utoipa::openapi::{schema::AnyOfBuilder, schema::ObjectBuilder, Type};
9
10// Empty arrays match several branches, so Serde's untagged union needs anyOf,
11// not oneOf. Do not add constraints from API prose that Serde does not enforce.
12schema!(
13    Prompt,
14    "async_openai.Prompt",
15    AnyOfBuilder::new()
16        .item(String::schema())
17        .item(Vec::<String>::schema())
18        .item(Vec::<u32>::schema())
19        .item(Vec::<Vec<u32>>::schema()),
20    []
21);
22
23schema!(
24    ChatCompletionFunctionCall,
25    "async_openai.ChatCompletionFunctionCall",
26    AnyOfBuilder::new()
27        .item(
28            ObjectBuilder::new()
29                .schema_type(Type::String)
30                .enum_values(Some(["none", "auto"]))
31        )
32        .item(FunctionName::schema()),
33    [FunctionName]
34);
35
36// The fallback accepts any string, including the named voices. Enumerating the
37// names alongside a string branch in oneOf would incorrectly reject them.
38schema!(
39    ChatCompletionAudioVoice,
40    "async_openai.ChatCompletionAudioVoice",
41    String::schema(),
42    []
43);
44
45// Only the object alternatives carry the "type" discriminator.
46#[derive(utoipa::ToSchema)]
47#[serde(tag = "type", rename_all = "snake_case")]
48#[allow(dead_code)]
49enum TaggedToolChoice {
50    AllowedTools(ChatCompletionAllowedToolsChoice),
51    Function(ChatCompletionNamedToolChoice),
52    Custom(ChatCompletionNamedToolChoiceCustom),
53}
54
55schema!(
56    ChatCompletionToolChoiceOption,
57    "async_openai.ChatCompletionToolChoiceOption",
58    AnyOfBuilder::new()
59        .item(TaggedToolChoice::schema())
60        .item(ToolChoiceOptions::schema()),
61    [TaggedToolChoice, ToolChoiceOptions]
62);