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

from .basesdk import BaseSDK
from enum import Enum
from openrouter import components, errors, operations, utils
from openrouter._hooks import HookContext
from openrouter.types import OptionalNullable, UNSET
from openrouter.utils import eventstreaming, get_security_from_env
from openrouter.utils.unmarshal_json_response import unmarshal_json_response
from typing import Any, Dict, List, Literal, Mapping, Optional, Union, overload


class SendAcceptEnum(str, Enum):
    APPLICATION_JSON = "application/json"
    TEXT_EVENT_STREAM = "text/event-stream"


class Chat(BaseSDK):
    @overload
    def send(
        self,
        *,
        messages: Union[
            List[components.ChatMessages], List[components.ChatMessagesTypedDict]
        ],
        http_referer: Optional[str] = None,
        x_open_router_title: Optional[str] = None,
        x_open_router_categories: Optional[str] = None,
        provider: OptionalNullable[
            Union[
                components.ChatRequestProvider, components.ChatRequestProviderTypedDict
            ]
        ] = UNSET,
        plugins: Optional[
            Union[
                List[components.ChatRequestPluginUnion],
                List[components.ChatRequestPluginUnionTypedDict],
            ]
        ] = None,
        user: Optional[str] = None,
        session_id: Optional[str] = None,
        trace: Optional[
            Union[components.ChatRequestTrace, components.ChatRequestTraceTypedDict]
        ] = None,
        model: Optional[str] = None,
        models: Optional[List[str]] = None,
        frequency_penalty: OptionalNullable[float] = UNSET,
        logit_bias: OptionalNullable[Dict[str, float]] = UNSET,
        logprobs: OptionalNullable[bool] = UNSET,
        top_logprobs: OptionalNullable[float] = UNSET,
        max_completion_tokens: OptionalNullable[float] = UNSET,
        max_tokens: OptionalNullable[float] = UNSET,
        metadata: Optional[Dict[str, str]] = None,
        presence_penalty: OptionalNullable[float] = UNSET,
        reasoning: Optional[
            Union[components.Reasoning, components.ReasoningTypedDict]
        ] = None,
        response_format: Optional[
            Union[components.ResponseFormat, components.ResponseFormatTypedDict]
        ] = None,
        seed: OptionalNullable[int] = UNSET,
        stop: OptionalNullable[
            Union[components.Stop, components.StopTypedDict]
        ] = UNSET,
        stream: Union[Literal[False], None] = None,
        stream_options: OptionalNullable[
            Union[components.ChatStreamOptions, components.ChatStreamOptionsTypedDict]
        ] = UNSET,
        temperature: OptionalNullable[float] = 1,
        parallel_tool_calls: OptionalNullable[bool] = UNSET,
        tool_choice: Optional[
            Union[components.ChatToolChoice, components.ChatToolChoiceTypedDict]
        ] = None,
        tools: Optional[
            Union[
                List[components.ChatFunctionTool],
                List[components.ChatFunctionToolTypedDict],
            ]
        ] = None,
        top_p: OptionalNullable[float] = 1,
        debug: Optional[
            Union[components.ChatDebugOptions, components.ChatDebugOptionsTypedDict]
        ] = None,
        image_config: Optional[
            Union[
                Dict[str, components.ChatRequestImageConfig],
                Dict[str, components.ChatRequestImageConfigTypedDict],
            ]
        ] = None,
        modalities: Optional[List[components.Modality]] = None,
        cache_control: Optional[
            Union[components.CacheControl, components.CacheControlTypedDict]
        ] = None,
        service_tier: OptionalNullable[components.ChatRequestServiceTier] = UNSET,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> components.ChatResult:
        r"""Create a chat completion

        Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

        :param messages: List of messages for the conversation
        :param http_referer: The app identifier should be your app's URL and is used as the primary identifier for rankings.
            This is used to track API usage per application.

        :param x_open_router_title: The app display name allows you to customize how your app appears in OpenRouter's dashboard.

        :param x_open_router_categories: Comma-separated list of app categories (e.g. \"cli-agent,cloud-agent\"). Used for marketplace rankings.

        :param provider: When multiple model providers are available, optionally indicate your routing preference.
        :param plugins: Plugins you want to enable for this request, including their settings.
        :param user: Unique user identifier
        :param session_id: 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.
        :param trace: 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.
        :param model: Model to use for completion
        :param models: Models to use for completion
        :param frequency_penalty: Frequency penalty (-2.0 to 2.0)
        :param logit_bias: Token logit bias adjustments
        :param logprobs: Return log probabilities
        :param top_logprobs: Number of top log probabilities to return (0-20)
        :param max_completion_tokens: Maximum tokens in completion
        :param max_tokens: Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16.
        :param metadata: Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)
        :param presence_penalty: Presence penalty (-2.0 to 2.0)
        :param reasoning: Configuration options for reasoning models
        :param response_format: Response format configuration
        :param seed: Random seed for deterministic outputs
        :param stop: Stop sequences (up to 4)
        :param stream: Enable streaming response
        :param stream_options: Streaming configuration options
        :param temperature: Sampling temperature (0-2)
        :param parallel_tool_calls:
        :param tool_choice: Tool choice configuration
        :param tools: Available tools for function calling
        :param top_p: Nucleus sampling parameter (0-1)
        :param debug: Debug options for inspecting request transformations (streaming only)
        :param image_config: 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.
        :param modalities: Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\".
        :param cache_control: 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.
        :param service_tier: The service tier to use for processing this request.
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """

    @overload
    def send(
        self,
        *,
        messages: Union[
            List[components.ChatMessages], List[components.ChatMessagesTypedDict]
        ],
        http_referer: Optional[str] = None,
        x_open_router_title: Optional[str] = None,
        x_open_router_categories: Optional[str] = None,
        provider: OptionalNullable[
            Union[
                components.ChatRequestProvider, components.ChatRequestProviderTypedDict
            ]
        ] = UNSET,
        plugins: Optional[
            Union[
                List[components.ChatRequestPluginUnion],
                List[components.ChatRequestPluginUnionTypedDict],
            ]
        ] = None,
        user: Optional[str] = None,
        session_id: Optional[str] = None,
        trace: Optional[
            Union[components.ChatRequestTrace, components.ChatRequestTraceTypedDict]
        ] = None,
        model: Optional[str] = None,
        models: Optional[List[str]] = None,
        frequency_penalty: OptionalNullable[float] = UNSET,
        logit_bias: OptionalNullable[Dict[str, float]] = UNSET,
        logprobs: OptionalNullable[bool] = UNSET,
        top_logprobs: OptionalNullable[float] = UNSET,
        max_completion_tokens: OptionalNullable[float] = UNSET,
        max_tokens: OptionalNullable[float] = UNSET,
        metadata: Optional[Dict[str, str]] = None,
        presence_penalty: OptionalNullable[float] = UNSET,
        reasoning: Optional[
            Union[components.Reasoning, components.ReasoningTypedDict]
        ] = None,
        response_format: Optional[
            Union[components.ResponseFormat, components.ResponseFormatTypedDict]
        ] = None,
        seed: OptionalNullable[int] = UNSET,
        stop: OptionalNullable[
            Union[components.Stop, components.StopTypedDict]
        ] = UNSET,
        stream: Literal[True],
        stream_options: OptionalNullable[
            Union[components.ChatStreamOptions, components.ChatStreamOptionsTypedDict]
        ] = UNSET,
        temperature: OptionalNullable[float] = 1,
        parallel_tool_calls: OptionalNullable[bool] = UNSET,
        tool_choice: Optional[
            Union[components.ChatToolChoice, components.ChatToolChoiceTypedDict]
        ] = None,
        tools: Optional[
            Union[
                List[components.ChatFunctionTool],
                List[components.ChatFunctionToolTypedDict],
            ]
        ] = None,
        top_p: OptionalNullable[float] = 1,
        debug: Optional[
            Union[components.ChatDebugOptions, components.ChatDebugOptionsTypedDict]
        ] = None,
        image_config: Optional[
            Union[
                Dict[str, components.ChatRequestImageConfig],
                Dict[str, components.ChatRequestImageConfigTypedDict],
            ]
        ] = None,
        modalities: Optional[List[components.Modality]] = None,
        cache_control: Optional[
            Union[components.CacheControl, components.CacheControlTypedDict]
        ] = None,
        service_tier: OptionalNullable[components.ChatRequestServiceTier] = UNSET,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> eventstreaming.EventStream[components.ChatStreamChunk]:
        r"""Create a chat completion

        Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

        :param messages: List of messages for the conversation
        :param http_referer: The app identifier should be your app's URL and is used as the primary identifier for rankings.
            This is used to track API usage per application.

        :param x_open_router_title: The app display name allows you to customize how your app appears in OpenRouter's dashboard.

        :param x_open_router_categories: Comma-separated list of app categories (e.g. \"cli-agent,cloud-agent\"). Used for marketplace rankings.

        :param provider: When multiple model providers are available, optionally indicate your routing preference.
        :param plugins: Plugins you want to enable for this request, including their settings.
        :param user: Unique user identifier
        :param session_id: 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.
        :param trace: 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.
        :param model: Model to use for completion
        :param models: Models to use for completion
        :param frequency_penalty: Frequency penalty (-2.0 to 2.0)
        :param logit_bias: Token logit bias adjustments
        :param logprobs: Return log probabilities
        :param top_logprobs: Number of top log probabilities to return (0-20)
        :param max_completion_tokens: Maximum tokens in completion
        :param max_tokens: Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16.
        :param metadata: Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)
        :param presence_penalty: Presence penalty (-2.0 to 2.0)
        :param reasoning: Configuration options for reasoning models
        :param response_format: Response format configuration
        :param seed: Random seed for deterministic outputs
        :param stop: Stop sequences (up to 4)
        :param stream: Enable streaming response
        :param stream_options: Streaming configuration options
        :param temperature: Sampling temperature (0-2)
        :param parallel_tool_calls:
        :param tool_choice: Tool choice configuration
        :param tools: Available tools for function calling
        :param top_p: Nucleus sampling parameter (0-1)
        :param debug: Debug options for inspecting request transformations (streaming only)
        :param image_config: 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.
        :param modalities: Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\".
        :param cache_control: 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.
        :param service_tier: The service tier to use for processing this request.
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """

    def send(
        self,
        *,
        messages: Union[
            List[components.ChatMessages], List[components.ChatMessagesTypedDict]
        ],
        http_referer: Optional[str] = None,
        x_open_router_title: Optional[str] = None,
        x_open_router_categories: Optional[str] = None,
        provider: OptionalNullable[
            Union[
                components.ChatRequestProvider, components.ChatRequestProviderTypedDict
            ]
        ] = UNSET,
        plugins: Optional[
            Union[
                List[components.ChatRequestPluginUnion],
                List[components.ChatRequestPluginUnionTypedDict],
            ]
        ] = None,
        user: Optional[str] = None,
        session_id: Optional[str] = None,
        trace: Optional[
            Union[components.ChatRequestTrace, components.ChatRequestTraceTypedDict]
        ] = None,
        model: Optional[str] = None,
        models: Optional[List[str]] = None,
        frequency_penalty: OptionalNullable[float] = UNSET,
        logit_bias: OptionalNullable[Dict[str, float]] = UNSET,
        logprobs: OptionalNullable[bool] = UNSET,
        top_logprobs: OptionalNullable[float] = UNSET,
        max_completion_tokens: OptionalNullable[float] = UNSET,
        max_tokens: OptionalNullable[float] = UNSET,
        metadata: Optional[Dict[str, str]] = None,
        presence_penalty: OptionalNullable[float] = UNSET,
        reasoning: Optional[
            Union[components.Reasoning, components.ReasoningTypedDict]
        ] = None,
        response_format: Optional[
            Union[components.ResponseFormat, components.ResponseFormatTypedDict]
        ] = None,
        seed: OptionalNullable[int] = UNSET,
        stop: OptionalNullable[
            Union[components.Stop, components.StopTypedDict]
        ] = UNSET,
        stream: Optional[bool] = False,
        stream_options: OptionalNullable[
            Union[components.ChatStreamOptions, components.ChatStreamOptionsTypedDict]
        ] = UNSET,
        temperature: OptionalNullable[float] = 1,
        parallel_tool_calls: OptionalNullable[bool] = UNSET,
        tool_choice: Optional[
            Union[components.ChatToolChoice, components.ChatToolChoiceTypedDict]
        ] = None,
        tools: Optional[
            Union[
                List[components.ChatFunctionTool],
                List[components.ChatFunctionToolTypedDict],
            ]
        ] = None,
        top_p: OptionalNullable[float] = 1,
        debug: Optional[
            Union[components.ChatDebugOptions, components.ChatDebugOptionsTypedDict]
        ] = None,
        image_config: Optional[
            Union[
                Dict[str, components.ChatRequestImageConfig],
                Dict[str, components.ChatRequestImageConfigTypedDict],
            ]
        ] = None,
        modalities: Optional[List[components.Modality]] = None,
        cache_control: Optional[
            Union[components.CacheControl, components.CacheControlTypedDict]
        ] = None,
        service_tier: OptionalNullable[components.ChatRequestServiceTier] = UNSET,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> operations.SendChatCompletionRequestResponse:
        r"""Create a chat completion

        Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

        :param messages: List of messages for the conversation
        :param http_referer: The app identifier should be your app's URL and is used as the primary identifier for rankings.
            This is used to track API usage per application.

        :param x_open_router_title: The app display name allows you to customize how your app appears in OpenRouter's dashboard.

        :param x_open_router_categories: Comma-separated list of app categories (e.g. \"cli-agent,cloud-agent\"). Used for marketplace rankings.

        :param provider: When multiple model providers are available, optionally indicate your routing preference.
        :param plugins: Plugins you want to enable for this request, including their settings.
        :param user: Unique user identifier
        :param session_id: 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.
        :param trace: 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.
        :param model: Model to use for completion
        :param models: Models to use for completion
        :param frequency_penalty: Frequency penalty (-2.0 to 2.0)
        :param logit_bias: Token logit bias adjustments
        :param logprobs: Return log probabilities
        :param top_logprobs: Number of top log probabilities to return (0-20)
        :param max_completion_tokens: Maximum tokens in completion
        :param max_tokens: Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16.
        :param metadata: Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)
        :param presence_penalty: Presence penalty (-2.0 to 2.0)
        :param reasoning: Configuration options for reasoning models
        :param response_format: Response format configuration
        :param seed: Random seed for deterministic outputs
        :param stop: Stop sequences (up to 4)
        :param stream: Enable streaming response
        :param stream_options: Streaming configuration options
        :param temperature: Sampling temperature (0-2)
        :param parallel_tool_calls:
        :param tool_choice: Tool choice configuration
        :param tools: Available tools for function calling
        :param top_p: Nucleus sampling parameter (0-1)
        :param debug: Debug options for inspecting request transformations (streaming only)
        :param image_config: 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.
        :param modalities: Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\".
        :param cache_control: 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.
        :param service_tier: The service tier to use for processing this request.
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = operations.SendChatCompletionRequestRequest(
            http_referer=http_referer,
            x_open_router_title=x_open_router_title,
            x_open_router_categories=x_open_router_categories,
            chat_request=components.ChatRequest(
                provider=utils.get_pydantic_model(
                    provider, OptionalNullable[components.ChatRequestProvider]
                ),
                plugins=utils.get_pydantic_model(
                    plugins, Optional[List[components.ChatRequestPluginUnion]]
                ),
                user=user,
                session_id=session_id,
                trace=utils.get_pydantic_model(
                    trace, Optional[components.ChatRequestTrace]
                ),
                messages=utils.get_pydantic_model(
                    messages, List[components.ChatMessages]
                ),
                model=model,
                models=models,
                frequency_penalty=frequency_penalty,
                logit_bias=logit_bias,
                logprobs=logprobs,
                top_logprobs=top_logprobs,
                max_completion_tokens=max_completion_tokens,
                max_tokens=max_tokens,
                metadata=metadata,
                presence_penalty=presence_penalty,
                reasoning=utils.get_pydantic_model(
                    reasoning, Optional[components.Reasoning]
                ),
                response_format=utils.get_pydantic_model(
                    response_format, Optional[components.ResponseFormat]
                ),
                seed=seed,
                stop=stop,
                stream=stream,
                stream_options=utils.get_pydantic_model(
                    stream_options, OptionalNullable[components.ChatStreamOptions]
                ),
                temperature=temperature,
                parallel_tool_calls=parallel_tool_calls,
                tool_choice=utils.get_pydantic_model(
                    tool_choice, Optional[components.ChatToolChoice]
                ),
                tools=utils.get_pydantic_model(
                    tools, Optional[List[components.ChatFunctionTool]]
                ),
                top_p=top_p,
                debug=utils.get_pydantic_model(
                    debug, Optional[components.ChatDebugOptions]
                ),
                image_config=image_config,
                modalities=modalities,
                cache_control=utils.get_pydantic_model(
                    cache_control, Optional[components.CacheControl]
                ),
                service_tier=service_tier,
            ),
        )

        req = self._build_request(
            method="POST",
            path="/chat/completions",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="text/event-stream" if stream else "application/json",
            http_headers=http_headers,
            _globals=operations.SendChatCompletionRequestGlobals(
                http_referer=self.sdk_configuration.globals.http_referer,
                x_open_router_title=self.sdk_configuration.globals.x_open_router_title,
                x_open_router_categories=self.sdk_configuration.globals.x_open_router_categories,
            ),
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request.chat_request, False, False, "json", components.ChatRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="sendChatCompletionRequest",
                oauth2_scopes=None,
                security_source=get_security_from_env(
                    self.sdk_configuration.security, components.Security
                ),
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "402",
                "404",
                "408",
                "413",
                "422",
                "429",
                "4XX",
                "500",
                "502",
                "503",
                "524",
                "529",
                "5XX",
            ],
            stream=True,
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            return unmarshal_json_response(
                components.ChatResult, http_res, http_res_text
            )
        if utils.match_response(http_res, "200", "text/event-stream"):
            return eventstreaming.EventStream(
                http_res,
                lambda raw: utils.unmarshal_json(
                    raw, operations.SendChatCompletionRequestResponseBody
                ).data,
                sentinel="[DONE]",
                client_ref=self,
            )
        if utils.match_response(http_res, "400", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.BadRequestResponseErrorData, http_res, http_res_text
            )
            raise errors.BadRequestResponseError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "401", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.UnauthorizedResponseErrorData, http_res, http_res_text
            )
            raise errors.UnauthorizedResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "402", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.PaymentRequiredResponseErrorData, http_res, http_res_text
            )
            raise errors.PaymentRequiredResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "404", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.NotFoundResponseErrorData, http_res, http_res_text
            )
            raise errors.NotFoundResponseError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "408", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.RequestTimeoutResponseErrorData, http_res, http_res_text
            )
            raise errors.RequestTimeoutResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "413", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.PayloadTooLargeResponseErrorData, http_res, http_res_text
            )
            raise errors.PayloadTooLargeResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "422", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityResponseErrorData, http_res, http_res_text
            )
            raise errors.UnprocessableEntityResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "429", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.TooManyRequestsResponseErrorData, http_res, http_res_text
            )
            raise errors.TooManyRequestsResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "500", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.InternalServerResponseErrorData, http_res, http_res_text
            )
            raise errors.InternalServerResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "502", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.BadGatewayResponseErrorData, http_res, http_res_text
            )
            raise errors.BadGatewayResponseError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "503", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableResponseErrorData, http_res, http_res_text
            )
            raise errors.ServiceUnavailableResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "524", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.EdgeNetworkTimeoutResponseErrorData, http_res, http_res_text
            )
            raise errors.EdgeNetworkTimeoutResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "529", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.ProviderOverloadedResponseErrorData, http_res, http_res_text
            )
            raise errors.ProviderOverloadedResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.OpenRouterDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.OpenRouterDefaultError(
                "API error occurred", http_res, http_res_text
            )

        http_res_text = utils.stream_to_text(http_res)
        raise errors.OpenRouterDefaultError(
            "Unexpected response received", http_res, http_res_text
        )

    @overload
    async def send_async(
        self,
        *,
        messages: Union[
            List[components.ChatMessages], List[components.ChatMessagesTypedDict]
        ],
        http_referer: Optional[str] = None,
        x_open_router_title: Optional[str] = None,
        x_open_router_categories: Optional[str] = None,
        provider: OptionalNullable[
            Union[
                components.ChatRequestProvider, components.ChatRequestProviderTypedDict
            ]
        ] = UNSET,
        plugins: Optional[
            Union[
                List[components.ChatRequestPluginUnion],
                List[components.ChatRequestPluginUnionTypedDict],
            ]
        ] = None,
        user: Optional[str] = None,
        session_id: Optional[str] = None,
        trace: Optional[
            Union[components.ChatRequestTrace, components.ChatRequestTraceTypedDict]
        ] = None,
        model: Optional[str] = None,
        models: Optional[List[str]] = None,
        frequency_penalty: OptionalNullable[float] = UNSET,
        logit_bias: OptionalNullable[Dict[str, float]] = UNSET,
        logprobs: OptionalNullable[bool] = UNSET,
        top_logprobs: OptionalNullable[float] = UNSET,
        max_completion_tokens: OptionalNullable[float] = UNSET,
        max_tokens: OptionalNullable[float] = UNSET,
        metadata: Optional[Dict[str, str]] = None,
        presence_penalty: OptionalNullable[float] = UNSET,
        reasoning: Optional[
            Union[components.Reasoning, components.ReasoningTypedDict]
        ] = None,
        response_format: Optional[
            Union[components.ResponseFormat, components.ResponseFormatTypedDict]
        ] = None,
        seed: OptionalNullable[int] = UNSET,
        stop: OptionalNullable[
            Union[components.Stop, components.StopTypedDict]
        ] = UNSET,
        stream: Union[Literal[False], None] = None,
        stream_options: OptionalNullable[
            Union[components.ChatStreamOptions, components.ChatStreamOptionsTypedDict]
        ] = UNSET,
        temperature: OptionalNullable[float] = 1,
        parallel_tool_calls: OptionalNullable[bool] = UNSET,
        tool_choice: Optional[
            Union[components.ChatToolChoice, components.ChatToolChoiceTypedDict]
        ] = None,
        tools: Optional[
            Union[
                List[components.ChatFunctionTool],
                List[components.ChatFunctionToolTypedDict],
            ]
        ] = None,
        top_p: OptionalNullable[float] = 1,
        debug: Optional[
            Union[components.ChatDebugOptions, components.ChatDebugOptionsTypedDict]
        ] = None,
        image_config: Optional[
            Union[
                Dict[str, components.ChatRequestImageConfig],
                Dict[str, components.ChatRequestImageConfigTypedDict],
            ]
        ] = None,
        modalities: Optional[List[components.Modality]] = None,
        cache_control: Optional[
            Union[components.CacheControl, components.CacheControlTypedDict]
        ] = None,
        service_tier: OptionalNullable[components.ChatRequestServiceTier] = UNSET,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> components.ChatResult:
        r"""Create a chat completion

        Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

        :param messages: List of messages for the conversation
        :param http_referer: The app identifier should be your app's URL and is used as the primary identifier for rankings.
            This is used to track API usage per application.

        :param x_open_router_title: The app display name allows you to customize how your app appears in OpenRouter's dashboard.

        :param x_open_router_categories: Comma-separated list of app categories (e.g. \"cli-agent,cloud-agent\"). Used for marketplace rankings.

        :param provider: When multiple model providers are available, optionally indicate your routing preference.
        :param plugins: Plugins you want to enable for this request, including their settings.
        :param user: Unique user identifier
        :param session_id: 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.
        :param trace: 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.
        :param model: Model to use for completion
        :param models: Models to use for completion
        :param frequency_penalty: Frequency penalty (-2.0 to 2.0)
        :param logit_bias: Token logit bias adjustments
        :param logprobs: Return log probabilities
        :param top_logprobs: Number of top log probabilities to return (0-20)
        :param max_completion_tokens: Maximum tokens in completion
        :param max_tokens: Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16.
        :param metadata: Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)
        :param presence_penalty: Presence penalty (-2.0 to 2.0)
        :param reasoning: Configuration options for reasoning models
        :param response_format: Response format configuration
        :param seed: Random seed for deterministic outputs
        :param stop: Stop sequences (up to 4)
        :param stream: Enable streaming response
        :param stream_options: Streaming configuration options
        :param temperature: Sampling temperature (0-2)
        :param parallel_tool_calls:
        :param tool_choice: Tool choice configuration
        :param tools: Available tools for function calling
        :param top_p: Nucleus sampling parameter (0-1)
        :param debug: Debug options for inspecting request transformations (streaming only)
        :param image_config: 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.
        :param modalities: Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\".
        :param cache_control: 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.
        :param service_tier: The service tier to use for processing this request.
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """

    @overload
    async def send_async(
        self,
        *,
        messages: Union[
            List[components.ChatMessages], List[components.ChatMessagesTypedDict]
        ],
        http_referer: Optional[str] = None,
        x_open_router_title: Optional[str] = None,
        x_open_router_categories: Optional[str] = None,
        provider: OptionalNullable[
            Union[
                components.ChatRequestProvider, components.ChatRequestProviderTypedDict
            ]
        ] = UNSET,
        plugins: Optional[
            Union[
                List[components.ChatRequestPluginUnion],
                List[components.ChatRequestPluginUnionTypedDict],
            ]
        ] = None,
        user: Optional[str] = None,
        session_id: Optional[str] = None,
        trace: Optional[
            Union[components.ChatRequestTrace, components.ChatRequestTraceTypedDict]
        ] = None,
        model: Optional[str] = None,
        models: Optional[List[str]] = None,
        frequency_penalty: OptionalNullable[float] = UNSET,
        logit_bias: OptionalNullable[Dict[str, float]] = UNSET,
        logprobs: OptionalNullable[bool] = UNSET,
        top_logprobs: OptionalNullable[float] = UNSET,
        max_completion_tokens: OptionalNullable[float] = UNSET,
        max_tokens: OptionalNullable[float] = UNSET,
        metadata: Optional[Dict[str, str]] = None,
        presence_penalty: OptionalNullable[float] = UNSET,
        reasoning: Optional[
            Union[components.Reasoning, components.ReasoningTypedDict]
        ] = None,
        response_format: Optional[
            Union[components.ResponseFormat, components.ResponseFormatTypedDict]
        ] = None,
        seed: OptionalNullable[int] = UNSET,
        stop: OptionalNullable[
            Union[components.Stop, components.StopTypedDict]
        ] = UNSET,
        stream: Literal[True],
        stream_options: OptionalNullable[
            Union[components.ChatStreamOptions, components.ChatStreamOptionsTypedDict]
        ] = UNSET,
        temperature: OptionalNullable[float] = 1,
        parallel_tool_calls: OptionalNullable[bool] = UNSET,
        tool_choice: Optional[
            Union[components.ChatToolChoice, components.ChatToolChoiceTypedDict]
        ] = None,
        tools: Optional[
            Union[
                List[components.ChatFunctionTool],
                List[components.ChatFunctionToolTypedDict],
            ]
        ] = None,
        top_p: OptionalNullable[float] = 1,
        debug: Optional[
            Union[components.ChatDebugOptions, components.ChatDebugOptionsTypedDict]
        ] = None,
        image_config: Optional[
            Union[
                Dict[str, components.ChatRequestImageConfig],
                Dict[str, components.ChatRequestImageConfigTypedDict],
            ]
        ] = None,
        modalities: Optional[List[components.Modality]] = None,
        cache_control: Optional[
            Union[components.CacheControl, components.CacheControlTypedDict]
        ] = None,
        service_tier: OptionalNullable[components.ChatRequestServiceTier] = UNSET,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> eventstreaming.EventStreamAsync[components.ChatStreamChunk]:
        r"""Create a chat completion

        Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

        :param messages: List of messages for the conversation
        :param http_referer: The app identifier should be your app's URL and is used as the primary identifier for rankings.
            This is used to track API usage per application.

        :param x_open_router_title: The app display name allows you to customize how your app appears in OpenRouter's dashboard.

        :param x_open_router_categories: Comma-separated list of app categories (e.g. \"cli-agent,cloud-agent\"). Used for marketplace rankings.

        :param provider: When multiple model providers are available, optionally indicate your routing preference.
        :param plugins: Plugins you want to enable for this request, including their settings.
        :param user: Unique user identifier
        :param session_id: 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.
        :param trace: 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.
        :param model: Model to use for completion
        :param models: Models to use for completion
        :param frequency_penalty: Frequency penalty (-2.0 to 2.0)
        :param logit_bias: Token logit bias adjustments
        :param logprobs: Return log probabilities
        :param top_logprobs: Number of top log probabilities to return (0-20)
        :param max_completion_tokens: Maximum tokens in completion
        :param max_tokens: Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16.
        :param metadata: Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)
        :param presence_penalty: Presence penalty (-2.0 to 2.0)
        :param reasoning: Configuration options for reasoning models
        :param response_format: Response format configuration
        :param seed: Random seed for deterministic outputs
        :param stop: Stop sequences (up to 4)
        :param stream: Enable streaming response
        :param stream_options: Streaming configuration options
        :param temperature: Sampling temperature (0-2)
        :param parallel_tool_calls:
        :param tool_choice: Tool choice configuration
        :param tools: Available tools for function calling
        :param top_p: Nucleus sampling parameter (0-1)
        :param debug: Debug options for inspecting request transformations (streaming only)
        :param image_config: 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.
        :param modalities: Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\".
        :param cache_control: 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.
        :param service_tier: The service tier to use for processing this request.
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """

    async def send_async(
        self,
        *,
        messages: Union[
            List[components.ChatMessages], List[components.ChatMessagesTypedDict]
        ],
        http_referer: Optional[str] = None,
        x_open_router_title: Optional[str] = None,
        x_open_router_categories: Optional[str] = None,
        provider: OptionalNullable[
            Union[
                components.ChatRequestProvider, components.ChatRequestProviderTypedDict
            ]
        ] = UNSET,
        plugins: Optional[
            Union[
                List[components.ChatRequestPluginUnion],
                List[components.ChatRequestPluginUnionTypedDict],
            ]
        ] = None,
        user: Optional[str] = None,
        session_id: Optional[str] = None,
        trace: Optional[
            Union[components.ChatRequestTrace, components.ChatRequestTraceTypedDict]
        ] = None,
        model: Optional[str] = None,
        models: Optional[List[str]] = None,
        frequency_penalty: OptionalNullable[float] = UNSET,
        logit_bias: OptionalNullable[Dict[str, float]] = UNSET,
        logprobs: OptionalNullable[bool] = UNSET,
        top_logprobs: OptionalNullable[float] = UNSET,
        max_completion_tokens: OptionalNullable[float] = UNSET,
        max_tokens: OptionalNullable[float] = UNSET,
        metadata: Optional[Dict[str, str]] = None,
        presence_penalty: OptionalNullable[float] = UNSET,
        reasoning: Optional[
            Union[components.Reasoning, components.ReasoningTypedDict]
        ] = None,
        response_format: Optional[
            Union[components.ResponseFormat, components.ResponseFormatTypedDict]
        ] = None,
        seed: OptionalNullable[int] = UNSET,
        stop: OptionalNullable[
            Union[components.Stop, components.StopTypedDict]
        ] = UNSET,
        stream: Optional[bool] = False,
        stream_options: OptionalNullable[
            Union[components.ChatStreamOptions, components.ChatStreamOptionsTypedDict]
        ] = UNSET,
        temperature: OptionalNullable[float] = 1,
        parallel_tool_calls: OptionalNullable[bool] = UNSET,
        tool_choice: Optional[
            Union[components.ChatToolChoice, components.ChatToolChoiceTypedDict]
        ] = None,
        tools: Optional[
            Union[
                List[components.ChatFunctionTool],
                List[components.ChatFunctionToolTypedDict],
            ]
        ] = None,
        top_p: OptionalNullable[float] = 1,
        debug: Optional[
            Union[components.ChatDebugOptions, components.ChatDebugOptionsTypedDict]
        ] = None,
        image_config: Optional[
            Union[
                Dict[str, components.ChatRequestImageConfig],
                Dict[str, components.ChatRequestImageConfigTypedDict],
            ]
        ] = None,
        modalities: Optional[List[components.Modality]] = None,
        cache_control: Optional[
            Union[components.CacheControl, components.CacheControlTypedDict]
        ] = None,
        service_tier: OptionalNullable[components.ChatRequestServiceTier] = UNSET,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> operations.SendChatCompletionRequestResponse:
        r"""Create a chat completion

        Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

        :param messages: List of messages for the conversation
        :param http_referer: The app identifier should be your app's URL and is used as the primary identifier for rankings.
            This is used to track API usage per application.

        :param x_open_router_title: The app display name allows you to customize how your app appears in OpenRouter's dashboard.

        :param x_open_router_categories: Comma-separated list of app categories (e.g. \"cli-agent,cloud-agent\"). Used for marketplace rankings.

        :param provider: When multiple model providers are available, optionally indicate your routing preference.
        :param plugins: Plugins you want to enable for this request, including their settings.
        :param user: Unique user identifier
        :param session_id: 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.
        :param trace: 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.
        :param model: Model to use for completion
        :param models: Models to use for completion
        :param frequency_penalty: Frequency penalty (-2.0 to 2.0)
        :param logit_bias: Token logit bias adjustments
        :param logprobs: Return log probabilities
        :param top_logprobs: Number of top log probabilities to return (0-20)
        :param max_completion_tokens: Maximum tokens in completion
        :param max_tokens: Maximum tokens (deprecated, use max_completion_tokens). Note: some providers enforce a minimum of 16.
        :param metadata: Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values)
        :param presence_penalty: Presence penalty (-2.0 to 2.0)
        :param reasoning: Configuration options for reasoning models
        :param response_format: Response format configuration
        :param seed: Random seed for deterministic outputs
        :param stop: Stop sequences (up to 4)
        :param stream: Enable streaming response
        :param stream_options: Streaming configuration options
        :param temperature: Sampling temperature (0-2)
        :param parallel_tool_calls:
        :param tool_choice: Tool choice configuration
        :param tools: Available tools for function calling
        :param top_p: Nucleus sampling parameter (0-1)
        :param debug: Debug options for inspecting request transformations (streaming only)
        :param image_config: 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.
        :param modalities: Output modalities for the response. Supported values are \"text\", \"image\", and \"audio\".
        :param cache_control: 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.
        :param service_tier: The service tier to use for processing this request.
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = operations.SendChatCompletionRequestRequest(
            http_referer=http_referer,
            x_open_router_title=x_open_router_title,
            x_open_router_categories=x_open_router_categories,
            chat_request=components.ChatRequest(
                provider=utils.get_pydantic_model(
                    provider, OptionalNullable[components.ChatRequestProvider]
                ),
                plugins=utils.get_pydantic_model(
                    plugins, Optional[List[components.ChatRequestPluginUnion]]
                ),
                user=user,
                session_id=session_id,
                trace=utils.get_pydantic_model(
                    trace, Optional[components.ChatRequestTrace]
                ),
                messages=utils.get_pydantic_model(
                    messages, List[components.ChatMessages]
                ),
                model=model,
                models=models,
                frequency_penalty=frequency_penalty,
                logit_bias=logit_bias,
                logprobs=logprobs,
                top_logprobs=top_logprobs,
                max_completion_tokens=max_completion_tokens,
                max_tokens=max_tokens,
                metadata=metadata,
                presence_penalty=presence_penalty,
                reasoning=utils.get_pydantic_model(
                    reasoning, Optional[components.Reasoning]
                ),
                response_format=utils.get_pydantic_model(
                    response_format, Optional[components.ResponseFormat]
                ),
                seed=seed,
                stop=stop,
                stream=stream,
                stream_options=utils.get_pydantic_model(
                    stream_options, OptionalNullable[components.ChatStreamOptions]
                ),
                temperature=temperature,
                parallel_tool_calls=parallel_tool_calls,
                tool_choice=utils.get_pydantic_model(
                    tool_choice, Optional[components.ChatToolChoice]
                ),
                tools=utils.get_pydantic_model(
                    tools, Optional[List[components.ChatFunctionTool]]
                ),
                top_p=top_p,
                debug=utils.get_pydantic_model(
                    debug, Optional[components.ChatDebugOptions]
                ),
                image_config=image_config,
                modalities=modalities,
                cache_control=utils.get_pydantic_model(
                    cache_control, Optional[components.CacheControl]
                ),
                service_tier=service_tier,
            ),
        )

        req = self._build_request_async(
            method="POST",
            path="/chat/completions",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="text/event-stream" if stream else "application/json",
            http_headers=http_headers,
            _globals=operations.SendChatCompletionRequestGlobals(
                http_referer=self.sdk_configuration.globals.http_referer,
                x_open_router_title=self.sdk_configuration.globals.x_open_router_title,
                x_open_router_categories=self.sdk_configuration.globals.x_open_router_categories,
            ),
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request.chat_request, False, False, "json", components.ChatRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="sendChatCompletionRequest",
                oauth2_scopes=None,
                security_source=get_security_from_env(
                    self.sdk_configuration.security, components.Security
                ),
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "402",
                "404",
                "408",
                "413",
                "422",
                "429",
                "4XX",
                "500",
                "502",
                "503",
                "524",
                "529",
                "5XX",
            ],
            stream=True,
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            return unmarshal_json_response(
                components.ChatResult, http_res, http_res_text
            )
        if utils.match_response(http_res, "200", "text/event-stream"):
            return eventstreaming.EventStreamAsync(
                http_res,
                lambda raw: utils.unmarshal_json(
                    raw, operations.SendChatCompletionRequestResponseBody
                ).data,
                sentinel="[DONE]",
                client_ref=self,
            )
        if utils.match_response(http_res, "400", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.BadRequestResponseErrorData, http_res, http_res_text
            )
            raise errors.BadRequestResponseError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "401", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.UnauthorizedResponseErrorData, http_res, http_res_text
            )
            raise errors.UnauthorizedResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "402", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.PaymentRequiredResponseErrorData, http_res, http_res_text
            )
            raise errors.PaymentRequiredResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "404", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.NotFoundResponseErrorData, http_res, http_res_text
            )
            raise errors.NotFoundResponseError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "408", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.RequestTimeoutResponseErrorData, http_res, http_res_text
            )
            raise errors.RequestTimeoutResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "413", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.PayloadTooLargeResponseErrorData, http_res, http_res_text
            )
            raise errors.PayloadTooLargeResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "422", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityResponseErrorData, http_res, http_res_text
            )
            raise errors.UnprocessableEntityResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "429", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.TooManyRequestsResponseErrorData, http_res, http_res_text
            )
            raise errors.TooManyRequestsResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "500", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.InternalServerResponseErrorData, http_res, http_res_text
            )
            raise errors.InternalServerResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "502", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.BadGatewayResponseErrorData, http_res, http_res_text
            )
            raise errors.BadGatewayResponseError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "503", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableResponseErrorData, http_res, http_res_text
            )
            raise errors.ServiceUnavailableResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "524", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.EdgeNetworkTimeoutResponseErrorData, http_res, http_res_text
            )
            raise errors.EdgeNetworkTimeoutResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "529", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.ProviderOverloadedResponseErrorData, http_res, http_res_text
            )
            raise errors.ProviderOverloadedResponseError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.OpenRouterDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.OpenRouterDefaultError(
                "API error occurred", http_res, http_res_text
            )

        http_res_text = await utils.stream_to_text_async(http_res)
        raise errors.OpenRouterDefaultError(
            "Unexpected response received", http_res, http_res_text
        )
