"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .chatdebugoptions import ChatDebugOptions, ChatDebugOptionsTypedDict
from .chatformatgrammarconfig import (
    ChatFormatGrammarConfig,
    ChatFormatGrammarConfigTypedDict,
)
from .chatformatjsonschemaconfig import (
    ChatFormatJSONSchemaConfig,
    ChatFormatJSONSchemaConfigTypedDict,
)
from .chatformatpythonconfig import (
    ChatFormatPythonConfig,
    ChatFormatPythonConfigTypedDict,
)
from .chatformattextconfig import ChatFormatTextConfig, ChatFormatTextConfigTypedDict
from .chatfunctiontool import ChatFunctionTool, ChatFunctionToolTypedDict
from .chatmessages import ChatMessages, ChatMessagesTypedDict
from .chatstreamoptions import ChatStreamOptions, ChatStreamOptionsTypedDict
from .chattoolchoice import ChatToolChoice, ChatToolChoiceTypedDict
from .contextcompressionengine import ContextCompressionEngine
from .datacollection import DataCollection
from .formatjsonobjectconfig import (
    FormatJSONObjectConfig,
    FormatJSONObjectConfigTypedDict,
)
from .pdfparseroptions import PDFParserOptions, PDFParserOptionsTypedDict
from .preferredmaxlatency import PreferredMaxLatency, PreferredMaxLatencyTypedDict
from .preferredminthroughput import (
    PreferredMinThroughput,
    PreferredMinThroughputTypedDict,
)
from .providername import ProviderName
from .quantization import Quantization
from .websearchengine import WebSearchEngine
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
    UnrecognizedStr,
)
from openrouter.utils import get_discriminator, validate_open_enum
import pydantic
from pydantic import ConfigDict, Discriminator, Tag, model_serializer
from pydantic.functional_validators import PlainValidator
from typing import Any, Dict, List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


ChatRequestOrderTypedDict = TypeAliasType(
    "ChatRequestOrderTypedDict", Union[ProviderName, str]
)


ChatRequestOrder = TypeAliasType(
    "ChatRequestOrder",
    Union[Annotated[ProviderName, PlainValidator(validate_open_enum(False))], str],
)


ChatRequestOnlyTypedDict = TypeAliasType(
    "ChatRequestOnlyTypedDict", Union[ProviderName, str]
)


ChatRequestOnly = TypeAliasType(
    "ChatRequestOnly",
    Union[Annotated[ProviderName, PlainValidator(validate_open_enum(False))], str],
)


ChatRequestIgnoreTypedDict = TypeAliasType(
    "ChatRequestIgnoreTypedDict", Union[ProviderName, str]
)


ChatRequestIgnore = TypeAliasType(
    "ChatRequestIgnore",
    Union[Annotated[ProviderName, PlainValidator(validate_open_enum(False))], str],
)


ChatRequestSortEnum = Union[
    Literal[
        "price",
        "throughput",
        "latency",
        "exacto",
    ],
    UnrecognizedStr,
]


ChatRequestProviderSortConfigEnum = Literal[
    "price",
    "throughput",
    "latency",
    "exacto",
]


ChatRequestBy = Union[
    Literal[
        "price",
        "throughput",
        "latency",
        "exacto",
    ],
    UnrecognizedStr,
]
r"""The provider sorting strategy (price, throughput, latency)"""


ChatRequestPartition = Union[
    Literal[
        "model",
        "none",
    ],
    UnrecognizedStr,
]
r"""Partitioning strategy for sorting: \"model\" (default) groups endpoints by model before sorting (fallback models remain fallbacks), \"none\" sorts all endpoints together regardless of model."""


class ChatRequestProviderSortConfigTypedDict(TypedDict):
    by: NotRequired[Nullable[ChatRequestBy]]
    r"""The provider sorting strategy (price, throughput, latency)"""
    partition: NotRequired[Nullable[ChatRequestPartition]]
    r"""Partitioning strategy for sorting: \"model\" (default) groups endpoints by model before sorting (fallback models remain fallbacks), \"none\" sorts all endpoints together regardless of model."""


class ChatRequestProviderSortConfig(BaseModel):
    by: Annotated[
        OptionalNullable[ChatRequestBy], PlainValidator(validate_open_enum(False))
    ] = UNSET
    r"""The provider sorting strategy (price, throughput, latency)"""

    partition: Annotated[
        OptionalNullable[ChatRequestPartition],
        PlainValidator(validate_open_enum(False)),
    ] = UNSET
    r"""Partitioning strategy for sorting: \"model\" (default) groups endpoints by model before sorting (fallback models remain fallbacks), \"none\" sorts all endpoints together regardless of model."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["by", "partition"]
        nullable_fields = ["by", "partition"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


ChatRequestProviderSortConfigUnionTypedDict = TypeAliasType(
    "ChatRequestProviderSortConfigUnionTypedDict",
    Union[ChatRequestProviderSortConfigTypedDict, ChatRequestProviderSortConfigEnum],
)


ChatRequestProviderSortConfigUnion = TypeAliasType(
    "ChatRequestProviderSortConfigUnion",
    Union[ChatRequestProviderSortConfig, ChatRequestProviderSortConfigEnum],
)


ChatRequestProviderSort = Union[
    Literal[
        "price",
        "throughput",
        "latency",
        "exacto",
    ],
    UnrecognizedStr,
]
r"""The provider sorting strategy (price, throughput, latency)"""


ChatRequestSortUnionTypedDict = TypeAliasType(
    "ChatRequestSortUnionTypedDict",
    Union[
        ChatRequestProviderSort,
        ChatRequestProviderSortConfigUnionTypedDict,
        ChatRequestSortEnum,
    ],
)
r"""The sorting strategy to use for this request, if \"order\" is not specified. When set, no load balancing is performed."""


ChatRequestSortUnion = TypeAliasType(
    "ChatRequestSortUnion",
    Union[
        Annotated[ChatRequestProviderSort, PlainValidator(validate_open_enum(False))],
        ChatRequestProviderSortConfigUnion,
        Annotated[ChatRequestSortEnum, PlainValidator(validate_open_enum(False))],
    ],
)
r"""The sorting strategy to use for this request, if \"order\" is not specified. When set, no load balancing is performed."""


class ChatRequestMaxPriceTypedDict(TypedDict):
    r"""The object specifying the maximum price you want to pay for this request. USD price per million tokens, for prompt and completion."""

    prompt: NotRequired[str]
    r"""Price per million prompt tokens"""
    completion: NotRequired[str]
    image: NotRequired[str]
    audio: NotRequired[str]
    request: NotRequired[str]


class ChatRequestMaxPrice(BaseModel):
    r"""The object specifying the maximum price you want to pay for this request. USD price per million tokens, for prompt and completion."""

    prompt: Optional[str] = None
    r"""Price per million prompt tokens"""

    completion: Optional[str] = None

    image: Optional[str] = None

    audio: Optional[str] = None

    request: Optional[str] = None


class ChatRequestProviderTypedDict(TypedDict):
    r"""When multiple model providers are available, optionally indicate your routing preference."""

    allow_fallbacks: NotRequired[Nullable[bool]]
    r"""Whether to allow backup providers to serve requests
    - true: (default) when the primary provider (or your custom providers in \"order\") is unavailable, use the next best provider.
    - false: use only the primary/custom provider, and return the upstream error if it's unavailable.

    """
    require_parameters: NotRequired[Nullable[bool]]
    r"""Whether to filter providers to only those that support the parameters you've provided. If this setting is omitted or set to false, then providers will receive only the parameters they support, and ignore the rest."""
    data_collection: NotRequired[Nullable[DataCollection]]
    r"""Data collection setting. If no available model provider meets the requirement, your request will return an error.
    - allow: (default) allow providers which store user data non-transiently and may train on it

    - deny: use only providers which do not collect user data.
    """
    zdr: NotRequired[Nullable[bool]]
    r"""Whether to restrict routing to only ZDR (Zero Data Retention) endpoints. When true, only endpoints that do not retain prompts will be used."""
    enforce_distillable_text: NotRequired[Nullable[bool]]
    r"""Whether to restrict routing to only models that allow text distillation. When true, only models where the author has allowed distillation will be used."""
    order: NotRequired[Nullable[List[ChatRequestOrderTypedDict]]]
    r"""An ordered list of provider slugs. The router will attempt to use the first provider in the subset of this list that supports your requested model, and fall back to the next if it is unavailable. If no providers are available, the request will fail with an error message."""
    only: NotRequired[Nullable[List[ChatRequestOnlyTypedDict]]]
    r"""List of provider slugs to allow. If provided, this list is merged with your account-wide allowed provider settings for this request."""
    ignore: NotRequired[Nullable[List[ChatRequestIgnoreTypedDict]]]
    r"""List of provider slugs to ignore. If provided, this list is merged with your account-wide ignored provider settings for this request."""
    quantizations: NotRequired[Nullable[List[Quantization]]]
    r"""A list of quantization levels to filter the provider by."""
    sort: NotRequired[Nullable[ChatRequestSortUnionTypedDict]]
    max_price: NotRequired[ChatRequestMaxPriceTypedDict]
    r"""The object specifying the maximum price you want to pay for this request. USD price per million tokens, for prompt and completion."""
    preferred_min_throughput: NotRequired[Nullable[PreferredMinThroughputTypedDict]]
    r"""Preferred minimum throughput (in tokens per second). Can be a number (applies to p50) or an object with percentile-specific cutoffs. Endpoints below the threshold(s) may still be used, but are deprioritized in routing. When using fallback models, this may cause a fallback model to be used instead of the primary model if it meets the threshold."""
    preferred_max_latency: NotRequired[Nullable[PreferredMaxLatencyTypedDict]]
    r"""Preferred maximum latency (in seconds). Can be a number (applies to p50) or an object with percentile-specific cutoffs. Endpoints above the threshold(s) may still be used, but are deprioritized in routing. When using fallback models, this may cause a fallback model to be used instead of the primary model if it meets the threshold."""


class ChatRequestProvider(BaseModel):
    r"""When multiple model providers are available, optionally indicate your routing preference."""

    allow_fallbacks: OptionalNullable[bool] = UNSET
    r"""Whether to allow backup providers to serve requests
    - true: (default) when the primary provider (or your custom providers in \"order\") is unavailable, use the next best provider.
    - false: use only the primary/custom provider, and return the upstream error if it's unavailable.

    """

    require_parameters: OptionalNullable[bool] = UNSET
    r"""Whether to filter providers to only those that support the parameters you've provided. If this setting is omitted or set to false, then providers will receive only the parameters they support, and ignore the rest."""

    data_collection: Annotated[
        OptionalNullable[DataCollection], PlainValidator(validate_open_enum(False))
    ] = UNSET
    r"""Data collection setting. If no available model provider meets the requirement, your request will return an error.
    - allow: (default) allow providers which store user data non-transiently and may train on it

    - deny: use only providers which do not collect user data.
    """

    zdr: OptionalNullable[bool] = UNSET
    r"""Whether to restrict routing to only ZDR (Zero Data Retention) endpoints. When true, only endpoints that do not retain prompts will be used."""

    enforce_distillable_text: OptionalNullable[bool] = UNSET
    r"""Whether to restrict routing to only models that allow text distillation. When true, only models where the author has allowed distillation will be used."""

    order: OptionalNullable[List[ChatRequestOrder]] = UNSET
    r"""An ordered list of provider slugs. The router will attempt to use the first provider in the subset of this list that supports your requested model, and fall back to the next if it is unavailable. If no providers are available, the request will fail with an error message."""

    only: OptionalNullable[List[ChatRequestOnly]] = UNSET
    r"""List of provider slugs to allow. If provided, this list is merged with your account-wide allowed provider settings for this request."""

    ignore: OptionalNullable[List[ChatRequestIgnore]] = UNSET
    r"""List of provider slugs to ignore. If provided, this list is merged with your account-wide ignored provider settings for this request."""

    quantizations: OptionalNullable[
        List[Annotated[Quantization, PlainValidator(validate_open_enum(False))]]
    ] = UNSET
    r"""A list of quantization levels to filter the provider by."""

    sort: OptionalNullable[ChatRequestSortUnion] = UNSET

    max_price: Optional[ChatRequestMaxPrice] = None
    r"""The object specifying the maximum price you want to pay for this request. USD price per million tokens, for prompt and completion."""

    preferred_min_throughput: OptionalNullable[PreferredMinThroughput] = UNSET
    r"""Preferred minimum throughput (in tokens per second). Can be a number (applies to p50) or an object with percentile-specific cutoffs. Endpoints below the threshold(s) may still be used, but are deprioritized in routing. When using fallback models, this may cause a fallback model to be used instead of the primary model if it meets the threshold."""

    preferred_max_latency: OptionalNullable[PreferredMaxLatency] = UNSET
    r"""Preferred maximum latency (in seconds). Can be a number (applies to p50) or an object with percentile-specific cutoffs. Endpoints above the threshold(s) may still be used, but are deprioritized in routing. When using fallback models, this may cause a fallback model to be used instead of the primary model if it meets the threshold."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "allow_fallbacks",
            "require_parameters",
            "data_collection",
            "zdr",
            "enforce_distillable_text",
            "order",
            "only",
            "ignore",
            "quantizations",
            "sort",
            "max_price",
            "preferred_min_throughput",
            "preferred_max_latency",
        ]
        nullable_fields = [
            "allow_fallbacks",
            "require_parameters",
            "data_collection",
            "zdr",
            "enforce_distillable_text",
            "order",
            "only",
            "ignore",
            "quantizations",
            "sort",
            "preferred_min_throughput",
            "preferred_max_latency",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


ChatRequestIDContextCompression = Literal["context-compression",]


class ChatRequestPluginContextCompressionTypedDict(TypedDict):
    id: ChatRequestIDContextCompression
    enabled: NotRequired[bool]
    r"""Set to false to disable the context-compression plugin for this request. Defaults to true."""
    engine: NotRequired[ContextCompressionEngine]
    r"""The compression engine to use. Defaults to \"middle-out\"."""


class ChatRequestPluginContextCompression(BaseModel):
    id: ChatRequestIDContextCompression

    enabled: Optional[bool] = None
    r"""Set to false to disable the context-compression plugin for this request. Defaults to true."""

    engine: Optional[ContextCompressionEngine] = None
    r"""The compression engine to use. Defaults to \"middle-out\"."""


ChatRequestIDResponseHealing = Literal["response-healing",]


class ChatRequestPluginResponseHealingTypedDict(TypedDict):
    id: ChatRequestIDResponseHealing
    enabled: NotRequired[bool]
    r"""Set to false to disable the response-healing plugin for this request. Defaults to true."""


class ChatRequestPluginResponseHealing(BaseModel):
    id: ChatRequestIDResponseHealing

    enabled: Optional[bool] = None
    r"""Set to false to disable the response-healing plugin for this request. Defaults to true."""


ChatRequestIDFileParser = Literal["file-parser",]


class ChatRequestPluginFileParserTypedDict(TypedDict):
    id: ChatRequestIDFileParser
    enabled: NotRequired[bool]
    r"""Set to false to disable the file-parser plugin for this request. Defaults to true."""
    pdf: NotRequired[PDFParserOptionsTypedDict]
    r"""Options for PDF parsing."""


class ChatRequestPluginFileParser(BaseModel):
    id: ChatRequestIDFileParser

    enabled: Optional[bool] = None
    r"""Set to false to disable the file-parser plugin for this request. Defaults to true."""

    pdf: Optional[PDFParserOptions] = None
    r"""Options for PDF parsing."""


ChatRequestIDWeb = Literal["web",]


class ChatRequestPluginWebTypedDict(TypedDict):
    id: ChatRequestIDWeb
    enabled: NotRequired[bool]
    r"""Set to false to disable the web-search plugin for this request. Defaults to true."""
    max_results: NotRequired[float]
    search_prompt: NotRequired[str]
    engine: NotRequired[WebSearchEngine]
    r"""The search engine to use for web search."""
    include_domains: NotRequired[List[str]]
    r"""A list of domains to restrict web search results to. Supports wildcards (e.g. \"*.substack.com\") and path filtering (e.g. \"openai.com/blog\")."""
    exclude_domains: NotRequired[List[str]]
    r"""A list of domains to exclude from web search results. Supports wildcards (e.g. \"*.substack.com\") and path filtering (e.g. \"openai.com/blog\")."""


class ChatRequestPluginWeb(BaseModel):
    id: ChatRequestIDWeb

    enabled: Optional[bool] = None
    r"""Set to false to disable the web-search plugin for this request. Defaults to true."""

    max_results: Optional[float] = None

    search_prompt: Optional[str] = None

    engine: Annotated[
        Optional[WebSearchEngine], PlainValidator(validate_open_enum(False))
    ] = None
    r"""The search engine to use for web search."""

    include_domains: Optional[List[str]] = None
    r"""A list of domains to restrict web search results to. Supports wildcards (e.g. \"*.substack.com\") and path filtering (e.g. \"openai.com/blog\")."""

    exclude_domains: Optional[List[str]] = None
    r"""A list of domains to exclude from web search results. Supports wildcards (e.g. \"*.substack.com\") and path filtering (e.g. \"openai.com/blog\")."""


ChatRequestIDModeration = Literal["moderation",]


class ChatRequestPluginModerationTypedDict(TypedDict):
    id: ChatRequestIDModeration


class ChatRequestPluginModeration(BaseModel):
    id: ChatRequestIDModeration


ChatRequestIDAutoRouter = Literal["auto-router",]


class ChatRequestPluginAutoRouterTypedDict(TypedDict):
    id: ChatRequestIDAutoRouter
    enabled: NotRequired[bool]
    r"""Set to false to disable the auto-router plugin for this request. Defaults to true."""
    allowed_models: NotRequired[List[str]]
    r"""List of model patterns to filter which models the auto-router can route between. Supports wildcards (e.g., \"anthropic/*\" matches all Anthropic models). When not specified, uses the default supported models list."""


class ChatRequestPluginAutoRouter(BaseModel):
    id: ChatRequestIDAutoRouter

    enabled: Optional[bool] = None
    r"""Set to false to disable the auto-router plugin for this request. Defaults to true."""

    allowed_models: Optional[List[str]] = None
    r"""List of model patterns to filter which models the auto-router can route between. Supports wildcards (e.g., \"anthropic/*\" matches all Anthropic models). When not specified, uses the default supported models list."""


ChatRequestPluginUnionTypedDict = TypeAliasType(
    "ChatRequestPluginUnionTypedDict",
    Union[
        ChatRequestPluginModerationTypedDict,
        ChatRequestPluginResponseHealingTypedDict,
        ChatRequestPluginAutoRouterTypedDict,
        ChatRequestPluginFileParserTypedDict,
        ChatRequestPluginContextCompressionTypedDict,
        ChatRequestPluginWebTypedDict,
    ],
)


ChatRequestPluginUnion = Annotated[
    Union[
        Annotated[ChatRequestPluginAutoRouter, Tag("auto-router")],
        Annotated[ChatRequestPluginModeration, Tag("moderation")],
        Annotated[ChatRequestPluginWeb, Tag("web")],
        Annotated[ChatRequestPluginFileParser, Tag("file-parser")],
        Annotated[ChatRequestPluginResponseHealing, Tag("response-healing")],
        Annotated[ChatRequestPluginContextCompression, Tag("context-compression")],
    ],
    Discriminator(lambda m: get_discriminator(m, "id", "id")),
]


class ChatRequestTraceTypedDict(TypedDict):
    r"""Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations."""

    trace_id: NotRequired[str]
    trace_name: NotRequired[str]
    span_name: NotRequired[str]
    generation_name: NotRequired[str]
    parent_span_id: NotRequired[str]


class ChatRequestTrace(BaseModel):
    r"""Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations."""

    model_config = ConfigDict(
        populate_by_name=True, arbitrary_types_allowed=True, extra="allow"
    )
    __pydantic_extra__: Dict[str, Nullable[Any]] = pydantic.Field(init=False)

    trace_id: Optional[str] = None

    trace_name: Optional[str] = None

    span_name: Optional[str] = None

    generation_name: Optional[str] = None

    parent_span_id: Optional[str] = None

    @property
    def additional_properties(self):
        return self.__pydantic_extra__

    @additional_properties.setter
    def additional_properties(self, value):
        self.__pydantic_extra__ = value  # pyright: ignore[reportIncompatibleVariableOverride]


Effort = Union[
    Literal[
        "xhigh",
        "high",
        "medium",
        "low",
        "minimal",
        "none",
    ],
    UnrecognizedStr,
]
r"""Constrains effort on reasoning for reasoning models"""


class ReasoningTypedDict(TypedDict):
    r"""Configuration options for reasoning models"""

    effort: NotRequired[Nullable[Effort]]
    r"""Constrains effort on reasoning for reasoning models"""
    summary: NotRequired[Nullable[Any]]


class Reasoning(BaseModel):
    r"""Configuration options for reasoning models"""

    effort: Annotated[
        OptionalNullable[Effort], PlainValidator(validate_open_enum(False))
    ] = UNSET
    r"""Constrains effort on reasoning for reasoning models"""

    summary: OptionalNullable[Any] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["effort", "summary"]
        nullable_fields = ["effort", "summary"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


ResponseFormatTypedDict = TypeAliasType(
    "ResponseFormatTypedDict",
    Union[
        ChatFormatTextConfigTypedDict,
        FormatJSONObjectConfigTypedDict,
        ChatFormatPythonConfigTypedDict,
        ChatFormatJSONSchemaConfigTypedDict,
        ChatFormatGrammarConfigTypedDict,
    ],
)
r"""Response format configuration"""


ResponseFormat = Annotated[
    Union[
        Annotated[ChatFormatTextConfig, Tag("text")],
        Annotated[FormatJSONObjectConfig, Tag("json_object")],
        Annotated[ChatFormatJSONSchemaConfig, Tag("json_schema")],
        Annotated[ChatFormatGrammarConfig, Tag("grammar")],
        Annotated[ChatFormatPythonConfig, Tag("python")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]
r"""Response format configuration"""


StopTypedDict = TypeAliasType("StopTypedDict", Union[str, List[str], Any])
r"""Stop sequences (up to 4)"""


Stop = TypeAliasType("Stop", Union[str, List[str], Any])
r"""Stop sequences (up to 4)"""


ChatRequestImageConfigTypedDict = TypeAliasType(
    "ChatRequestImageConfigTypedDict", Union[str, float, List[Nullable[Any]]]
)


ChatRequestImageConfig = TypeAliasType(
    "ChatRequestImageConfig", Union[str, float, List[Nullable[Any]]]
)


Modality = Union[
    Literal[
        "text",
        "image",
        "audio",
    ],
    UnrecognizedStr,
]


ChatRequestType = Literal["ephemeral",]


ChatRequestTTL = Union[
    Literal[
        "5m",
        "1h",
    ],
    UnrecognizedStr,
]


class CacheControlTypedDict(TypedDict):
    r"""Enable automatic prompt caching. When set, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""

    type: ChatRequestType
    ttl: NotRequired[ChatRequestTTL]


class CacheControl(BaseModel):
    r"""Enable automatic prompt caching. When set, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""

    type: ChatRequestType

    ttl: Annotated[
        Optional[ChatRequestTTL], PlainValidator(validate_open_enum(False))
    ] = None


ChatRequestServiceTier = Union[
    Literal[
        "auto",
        "default",
        "flex",
        "priority",
        "scale",
    ],
    UnrecognizedStr,
]
r"""The service tier to use for processing this request."""


class ChatRequestTypedDict(TypedDict):
    r"""Chat completion request parameters"""

    messages: List[ChatMessagesTypedDict]
    r"""List of messages for the conversation"""
    provider: NotRequired[Nullable[ChatRequestProviderTypedDict]]
    r"""When multiple model providers are available, optionally indicate your routing preference."""
    plugins: NotRequired[List[ChatRequestPluginUnionTypedDict]]
    r"""Plugins you want to enable for this request, including their settings."""
    user: NotRequired[str]
    r"""Unique user identifier"""
    session_id: NotRequired[str]
    r"""A unique identifier for grouping related requests (e.g., a conversation or agent workflow) for observability. If provided in both the request body and the x-session-id header, the body value takes precedence. Maximum of 256 characters."""
    trace: NotRequired[ChatRequestTraceTypedDict]
    r"""Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations."""
    model: NotRequired[str]
    r"""Model to use for completion"""
    models: NotRequired[List[str]]
    r"""Models to use for completion"""
    frequency_penalty: NotRequired[Nullable[float]]
    r"""Frequency penalty (-2.0 to 2.0)"""
    logit_bias: NotRequired[Nullable[Dict[str, float]]]
    r"""Token logit bias adjustments"""
    logprobs: NotRequired[Nullable[bool]]
    r"""Return log probabilities"""
    top_logprobs: NotRequired[Nullable[float]]
    r"""Number of top log probabilities to return (0-20)"""
    max_completion_tokens: NotRequired[Nullable[float]]
    r"""Maximum tokens in completion"""
    max_tokens: NotRequired[Nullable[float]]
    r"""Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16."""
    metadata: NotRequired[Dict[str, str]]
    r"""Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)"""
    presence_penalty: NotRequired[Nullable[float]]
    r"""Presence penalty (-2.0 to 2.0)"""
    reasoning: NotRequired[ReasoningTypedDict]
    r"""Configuration options for reasoning models"""
    response_format: NotRequired[ResponseFormatTypedDict]
    r"""Response format configuration"""
    seed: NotRequired[Nullable[int]]
    r"""Random seed for deterministic outputs"""
    stop: NotRequired[Nullable[StopTypedDict]]
    r"""Stop sequences (up to 4)"""
    stream: NotRequired[bool]
    r"""Enable streaming response"""
    stream_options: NotRequired[Nullable[ChatStreamOptionsTypedDict]]
    r"""Streaming configuration options"""
    temperature: NotRequired[Nullable[float]]
    r"""Sampling temperature (0-2)"""
    parallel_tool_calls: NotRequired[Nullable[bool]]
    tool_choice: NotRequired[ChatToolChoiceTypedDict]
    r"""Tool choice configuration"""
    tools: NotRequired[List[ChatFunctionToolTypedDict]]
    r"""Available tools for function calling"""
    top_p: NotRequired[Nullable[float]]
    r"""Nucleus sampling parameter (0-1)"""
    debug: NotRequired[ChatDebugOptionsTypedDict]
    r"""Debug options for inspecting request transformations (streaming only)"""
    image_config: NotRequired[Dict[str, ChatRequestImageConfigTypedDict]]
    r"""Provider-specific image configuration options. Keys and values vary by model/provider. See https://openrouter.ai/docs/guides/overview/multimodal/image-generation for more details."""
    modalities: NotRequired[List[Modality]]
    r"""Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\"."""
    cache_control: NotRequired[CacheControlTypedDict]
    r"""Enable automatic prompt caching. When set, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""
    service_tier: NotRequired[Nullable[ChatRequestServiceTier]]
    r"""The service tier to use for processing this request."""


class ChatRequest(BaseModel):
    r"""Chat completion request parameters"""

    messages: List[ChatMessages]
    r"""List of messages for the conversation"""

    provider: OptionalNullable[ChatRequestProvider] = UNSET
    r"""When multiple model providers are available, optionally indicate your routing preference."""

    plugins: Optional[List[ChatRequestPluginUnion]] = None
    r"""Plugins you want to enable for this request, including their settings."""

    user: Optional[str] = None
    r"""Unique user identifier"""

    session_id: Optional[str] = None
    r"""A unique identifier for grouping related requests (e.g., a conversation or agent workflow) for observability. If provided in both the request body and the x-session-id header, the body value takes precedence. Maximum of 256 characters."""

    trace: Optional[ChatRequestTrace] = None
    r"""Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations."""

    model: Optional[str] = None
    r"""Model to use for completion"""

    models: Optional[List[str]] = None
    r"""Models to use for completion"""

    frequency_penalty: OptionalNullable[float] = UNSET
    r"""Frequency penalty (-2.0 to 2.0)"""

    logit_bias: OptionalNullable[Dict[str, float]] = UNSET
    r"""Token logit bias adjustments"""

    logprobs: OptionalNullable[bool] = UNSET
    r"""Return log probabilities"""

    top_logprobs: OptionalNullable[float] = UNSET
    r"""Number of top log probabilities to return (0-20)"""

    max_completion_tokens: OptionalNullable[float] = UNSET
    r"""Maximum tokens in completion"""

    max_tokens: OptionalNullable[float] = UNSET
    r"""Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16."""

    metadata: Optional[Dict[str, str]] = None
    r"""Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)"""

    presence_penalty: OptionalNullable[float] = UNSET
    r"""Presence penalty (-2.0 to 2.0)"""

    reasoning: Optional[Reasoning] = None
    r"""Configuration options for reasoning models"""

    response_format: Optional[ResponseFormat] = None
    r"""Response format configuration"""

    seed: OptionalNullable[int] = UNSET
    r"""Random seed for deterministic outputs"""

    stop: OptionalNullable[Stop] = UNSET
    r"""Stop sequences (up to 4)"""

    stream: Optional[bool] = False
    r"""Enable streaming response"""

    stream_options: OptionalNullable[ChatStreamOptions] = UNSET
    r"""Streaming configuration options"""

    temperature: OptionalNullable[float] = 1
    r"""Sampling temperature (0-2)"""

    parallel_tool_calls: OptionalNullable[bool] = UNSET

    tool_choice: Optional[ChatToolChoice] = None
    r"""Tool choice configuration"""

    tools: Optional[List[ChatFunctionTool]] = None
    r"""Available tools for function calling"""

    top_p: OptionalNullable[float] = 1
    r"""Nucleus sampling parameter (0-1)"""

    debug: Optional[ChatDebugOptions] = None
    r"""Debug options for inspecting request transformations (streaming only)"""

    image_config: Optional[Dict[str, ChatRequestImageConfig]] = None
    r"""Provider-specific image configuration options. Keys and values vary by model/provider. See https://openrouter.ai/docs/guides/overview/multimodal/image-generation for more details."""

    modalities: Optional[
        List[Annotated[Modality, PlainValidator(validate_open_enum(False))]]
    ] = None
    r"""Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\"."""

    cache_control: Optional[CacheControl] = None
    r"""Enable automatic prompt caching. When set, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""

    service_tier: Annotated[
        OptionalNullable[ChatRequestServiceTier],
        PlainValidator(validate_open_enum(False)),
    ] = UNSET
    r"""The service tier to use for processing this request."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "provider",
            "plugins",
            "user",
            "session_id",
            "trace",
            "model",
            "models",
            "frequency_penalty",
            "logit_bias",
            "logprobs",
            "top_logprobs",
            "max_completion_tokens",
            "max_tokens",
            "metadata",
            "presence_penalty",
            "reasoning",
            "response_format",
            "seed",
            "stop",
            "stream",
            "stream_options",
            "temperature",
            "parallel_tool_calls",
            "tool_choice",
            "tools",
            "top_p",
            "debug",
            "image_config",
            "modalities",
            "cache_control",
            "service_tier",
        ]
        nullable_fields = [
            "provider",
            "frequency_penalty",
            "logit_bias",
            "logprobs",
            "top_logprobs",
            "max_completion_tokens",
            "max_tokens",
            "presence_penalty",
            "seed",
            "stop",
            "stream_options",
            "temperature",
            "parallel_tool_calls",
            "top_p",
            "service_tier",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
