Skip to main content

dynamo_protocols/
schema.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Schema-only adapters and placeholders for protocol types.
5//!
6//! These placeholders let Dynamo-owned types derive `ToSchema` without
7//! requiring `async-openai/protocol-schema`. Field-level `schema(value_type = ...)`
8//! annotations select them for schema generation only; runtime field types and
9//! Serde behavior remain unchanged.
10//!
11//! Each placeholder preserves the upstream type's identity through an
12//! `x-dynamo-schema-import` marker, but does not describe its detailed shape.
13//! Consumers must resolve these markers against version-aligned upstream
14//! schemas before claiming complete coverage. This module does not perform
15//! that resolution or merge specifications.
16//!
17//! Once native upstream schema support is integrated, the corresponding
18//! placeholders and field overrides can be removed.
19
20macro_rules! imports {
21    ($($name:ident),* $(,)?) => {$(
22        pub struct $name;
23
24        impl utoipa::PartialSchema for $name {
25            fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::Schema> {
26                use utoipa::openapi::schema::{ObjectBuilder, SchemaType, Type};
27                ObjectBuilder::new()
28                    // These imported Rust types are non-Option. Excluding null
29                    // prevents overlap with utoipa's Option<T> oneOf null arm.
30                    .schema_type(SchemaType::Array(vec![
31                        Type::Object, Type::Array, Type::String,
32                        Type::Number, Type::Boolean,
33                    ]))
34                    .extensions(Some([(
35                        "x-dynamo-schema-import",
36                        serde_json::json!({"crate": "async-openai", "type": stringify!($name)}),
37                    )].into_iter().collect()))
38                    .into()
39            }
40        }
41
42        impl utoipa::ToSchema for $name {
43            fn name() -> std::borrow::Cow<'static, str> {
44                concat!("async_openai.", stringify!($name)).into()
45            }
46        }
47    )*};
48}
49
50imports!(
51    FunctionType,
52    ImageDetail,
53    ChatCompletionRequestMessageContentPartText,
54    ChatCompletionRequestMessageContentPartAudio,
55    ChatCompletionRequestSystemMessageContent,
56    ChatCompletionRequestAssistantMessageContent,
57    ChatCompletionRequestAssistantMessageAudio,
58    ChatCompletionRequestDeveloperMessage,
59    ChatCompletionRequestFunctionMessage,
60    Role,
61    ChatCompletionResponseMessageAudio,
62    ResponseModalities,
63    PredictionContent,
64    ChatCompletionAudio,
65    ResponseFormat,
66    ServiceTier,
67    ChatCompletionFunctionCall,
68    ChatCompletionFunctions,
69    WebSearchOptions,
70    FinishReason,
71    CompletionUsage,
72    Prompt,
73);
74
75/// Includes the enum discriminator so closed schemas accept tagged Custom parts.
76#[derive(utoipa::ToSchema)]
77#[serde(deny_unknown_fields)]
78#[schema(as = dynamo_protocols::chat::CustomContentPart)]
79pub struct CustomContentPart {
80    pub r#type: String,
81    pub kind: String,
82    pub fields: serde_json::Map<String, serde_json::Value>,
83}