mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-08-01 12:40:23 +08:00
This commit is contained in:
+175
-573
@@ -1,6 +1,7 @@
|
||||
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
||||
|
||||
from .basesdk import BaseSDK
|
||||
from enum import Enum
|
||||
from openrouter import errors, models, utils
|
||||
from openrouter._hooks import HookContext
|
||||
from openrouter.types import OptionalNullable, UNSET
|
||||
@@ -8,9 +9,120 @@ 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, Mapping, Optional, Union
|
||||
|
||||
# region imports
|
||||
from typing import overload
|
||||
from typing_extensions import Literal
|
||||
# endregion imports
|
||||
|
||||
|
||||
class CompleteAcceptEnum(str, Enum):
|
||||
APPLICATION_JSON = "application/json"
|
||||
TEXT_EVENT_STREAM = "text/event-stream"
|
||||
|
||||
|
||||
class Chat(BaseSDK):
|
||||
r"""Chat completion operations"""
|
||||
# region sdk-class-body
|
||||
@overload
|
||||
def complete(
|
||||
self,
|
||||
*,
|
||||
messages: Union[
|
||||
List[models.ChatCompletionMessageParam],
|
||||
List[models.ChatCompletionMessageParamTypedDict],
|
||||
],
|
||||
model: Optional[str] = None,
|
||||
stream: Literal[True],
|
||||
stream_options: OptionalNullable[
|
||||
Union[models.StreamOptions, models.StreamOptionsTypedDict]
|
||||
] = UNSET,
|
||||
temperature: OptionalNullable[float] = 1,
|
||||
tool_choice: Optional[
|
||||
Union[
|
||||
models.ChatCompletionToolChoiceOption,
|
||||
models.ChatCompletionToolChoiceOptionTypedDict,
|
||||
]
|
||||
] = None,
|
||||
tools: Optional[
|
||||
Union[
|
||||
List[models.ChatCompletionTool],
|
||||
List[models.ChatCompletionToolTypedDict],
|
||||
]
|
||||
] = None,
|
||||
top_p: OptionalNullable[float] = 1,
|
||||
user: Optional[str] = None,
|
||||
model_list: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[models.ReasoningEffort] = UNSET,
|
||||
provider: OptionalNullable[
|
||||
Union[models.Provider, models.ProviderTypedDict]
|
||||
] = UNSET,
|
||||
plugins: Optional[
|
||||
Union[List[models.Plugin], List[models.PluginTypedDict]]
|
||||
] = None,
|
||||
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
||||
server_url: Optional[str] = None,
|
||||
timeout_ms: Optional[int] = None,
|
||||
accept_header_override: Optional[CompleteAcceptEnum] = None,
|
||||
http_headers: Optional[Mapping[str, str]] = None,
|
||||
) -> eventstreaming.EventStream[models.ChatCompletionChunkWrapper]: ...
|
||||
@overload
|
||||
def complete(
|
||||
self,
|
||||
*,
|
||||
messages: Union[
|
||||
List[models.ChatCompletionMessageParam],
|
||||
List[models.ChatCompletionMessageParamTypedDict],
|
||||
],
|
||||
model: Optional[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: OptionalNullable[
|
||||
Union[models.Reasoning, models.ReasoningTypedDict]
|
||||
] = UNSET,
|
||||
response_format: Optional[
|
||||
Union[models.ResponseFormat, models.ResponseFormatTypedDict]
|
||||
] = None,
|
||||
seed: OptionalNullable[int] = UNSET,
|
||||
stop: OptionalNullable[Union[models.Stop, models.StopTypedDict]] = UNSET,
|
||||
stream: Union[Literal[False], None] = None,
|
||||
stream_options: OptionalNullable[
|
||||
Union[models.StreamOptions, models.StreamOptionsTypedDict]
|
||||
] = UNSET,
|
||||
temperature: OptionalNullable[float] = 1,
|
||||
tool_choice: Optional[
|
||||
Union[
|
||||
models.ChatCompletionToolChoiceOption,
|
||||
models.ChatCompletionToolChoiceOptionTypedDict,
|
||||
]
|
||||
] = None,
|
||||
tools: Optional[
|
||||
Union[
|
||||
List[models.ChatCompletionTool],
|
||||
List[models.ChatCompletionToolTypedDict],
|
||||
]
|
||||
] = None,
|
||||
top_p: OptionalNullable[float] = 1,
|
||||
user: Optional[str] = None,
|
||||
model_list: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[models.ReasoningEffort] = UNSET,
|
||||
provider: OptionalNullable[
|
||||
Union[models.Provider, models.ProviderTypedDict]
|
||||
] = UNSET,
|
||||
plugins: Optional[
|
||||
Union[List[models.Plugin], List[models.PluginTypedDict]]
|
||||
] = None,
|
||||
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
||||
server_url: Optional[str] = None,
|
||||
timeout_ms: Optional[int] = None,
|
||||
accept_header_override: Optional[CompleteAcceptEnum] = None,
|
||||
http_headers: Optional[Mapping[str, str]] = None,
|
||||
) -> models.ChatCompletion: ...
|
||||
# endregion sdk-class-body
|
||||
|
||||
def complete(
|
||||
self,
|
||||
@@ -29,30 +141,16 @@ class Chat(BaseSDK):
|
||||
metadata: Optional[Dict[str, str]] = None,
|
||||
presence_penalty: OptionalNullable[float] = UNSET,
|
||||
reasoning: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsReasoning,
|
||||
models.ChatCompletionCreateParamsReasoningTypedDict,
|
||||
]
|
||||
Union[models.Reasoning, models.ReasoningTypedDict]
|
||||
] = UNSET,
|
||||
response_format: Optional[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsResponseFormatUnion,
|
||||
models.ChatCompletionCreateParamsResponseFormatUnionTypedDict,
|
||||
]
|
||||
Union[models.ResponseFormat, models.ResponseFormatTypedDict]
|
||||
] = None,
|
||||
seed: OptionalNullable[int] = UNSET,
|
||||
stop: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsStop,
|
||||
models.ChatCompletionCreateParamsStopTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
stop: OptionalNullable[Union[models.Stop, models.StopTypedDict]] = UNSET,
|
||||
stream: OptionalNullable[bool] = False,
|
||||
stream_options: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsStreamOptions,
|
||||
models.ChatCompletionCreateParamsStreamOptionsTypedDict,
|
||||
]
|
||||
Union[models.StreamOptions, models.StreamOptionsTypedDict]
|
||||
] = UNSET,
|
||||
temperature: OptionalNullable[float] = 1,
|
||||
tool_choice: Optional[
|
||||
@@ -69,27 +167,20 @@ class Chat(BaseSDK):
|
||||
] = None,
|
||||
top_p: OptionalNullable[float] = 1,
|
||||
user: Optional[str] = None,
|
||||
models_llm: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[
|
||||
models.ChatCompletionCreateParamsReasoningEffort
|
||||
] = UNSET,
|
||||
model_list: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[models.ReasoningEffort] = UNSET,
|
||||
provider: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsProvider,
|
||||
models.ChatCompletionCreateParamsProviderTypedDict,
|
||||
]
|
||||
Union[models.Provider, models.ProviderTypedDict]
|
||||
] = UNSET,
|
||||
plugins: Optional[
|
||||
Union[
|
||||
List[models.ChatCompletionCreateParamsPluginUnion],
|
||||
List[models.ChatCompletionCreateParamsPluginUnionTypedDict],
|
||||
]
|
||||
Union[List[models.Plugin], List[models.PluginTypedDict]]
|
||||
] = None,
|
||||
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
||||
server_url: Optional[str] = None,
|
||||
timeout_ms: Optional[int] = None,
|
||||
accept_header_override: Optional[CompleteAcceptEnum] = None,
|
||||
http_headers: Optional[Mapping[str, str]] = None,
|
||||
) -> models.ChatCompletion:
|
||||
) -> models.CreateChatCompletionResponse:
|
||||
r"""Create a chat completion
|
||||
|
||||
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
|
||||
@@ -115,13 +206,14 @@ class Chat(BaseSDK):
|
||||
:param tools: Available tools for function calling
|
||||
:param top_p: Nucleus sampling parameter (0-1)
|
||||
:param user: Unique user identifier
|
||||
:param models_llm: Order of models to fallback to for this request
|
||||
:param model_list: Order of models to fallback to for this request
|
||||
:param reasoning_effort: Reasoning effort
|
||||
: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 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
|
||||
@@ -148,18 +240,16 @@ class Chat(BaseSDK):
|
||||
metadata=metadata,
|
||||
presence_penalty=presence_penalty,
|
||||
reasoning=utils.get_pydantic_model(
|
||||
reasoning, OptionalNullable[models.ChatCompletionCreateParamsReasoning]
|
||||
reasoning, OptionalNullable[models.Reasoning]
|
||||
),
|
||||
response_format=utils.get_pydantic_model(
|
||||
response_format,
|
||||
Optional[models.ChatCompletionCreateParamsResponseFormatUnion],
|
||||
response_format, Optional[models.ResponseFormat]
|
||||
),
|
||||
seed=seed,
|
||||
stop=stop,
|
||||
stream=stream,
|
||||
stream_options=utils.get_pydantic_model(
|
||||
stream_options,
|
||||
OptionalNullable[models.ChatCompletionCreateParamsStreamOptions],
|
||||
stream_options, OptionalNullable[models.StreamOptions]
|
||||
),
|
||||
temperature=temperature,
|
||||
tool_choice=utils.get_pydantic_model(
|
||||
@@ -170,14 +260,12 @@ class Chat(BaseSDK):
|
||||
),
|
||||
top_p=top_p,
|
||||
user=user,
|
||||
models_llm=models_llm,
|
||||
model_list=model_list,
|
||||
reasoning_effort=reasoning_effort,
|
||||
provider=utils.get_pydantic_model(
|
||||
provider, OptionalNullable[models.ChatCompletionCreateParamsProvider]
|
||||
),
|
||||
plugins=utils.get_pydantic_model(
|
||||
plugins, Optional[List[models.ChatCompletionCreateParamsPluginUnion]]
|
||||
provider, OptionalNullable[models.Provider]
|
||||
),
|
||||
plugins=utils.get_pydantic_model(plugins, Optional[List[models.Plugin]]),
|
||||
)
|
||||
|
||||
req = self._build_request(
|
||||
@@ -190,7 +278,9 @@ class Chat(BaseSDK):
|
||||
request_has_path_params=False,
|
||||
request_has_query_params=True,
|
||||
user_agent_header="user-agent",
|
||||
accept_header_value="application/json",
|
||||
accept_header_value=accept_header_override.value
|
||||
if accept_header_override is not None
|
||||
else "application/json;q=1, text/event-stream;q=0",
|
||||
http_headers=http_headers,
|
||||
security=self.sdk_configuration.security,
|
||||
get_serialized_body=lambda: utils.serialize_request_body(
|
||||
@@ -219,493 +309,24 @@ class Chat(BaseSDK):
|
||||
),
|
||||
request=req,
|
||||
error_status_codes=["400", "401", "429", "4XX", "500", "5XX"],
|
||||
retry_config=retry_config,
|
||||
)
|
||||
|
||||
response_data: Any = None
|
||||
if utils.match_response(http_res, "200", "application/json"):
|
||||
return unmarshal_json_response(models.ChatCompletion, http_res)
|
||||
if utils.match_response(http_res, ["400", "401", "429"], "application/json"):
|
||||
response_data = unmarshal_json_response(
|
||||
errors.ChatCompletionErrorData, http_res
|
||||
)
|
||||
raise errors.ChatCompletionError(response_data, http_res)
|
||||
if utils.match_response(http_res, "500", "application/json"):
|
||||
response_data = unmarshal_json_response(
|
||||
errors.ChatCompletionErrorData, http_res
|
||||
)
|
||||
raise errors.ChatCompletionError(response_data, http_res)
|
||||
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
|
||||
)
|
||||
|
||||
raise errors.OpenRouterDefaultError("Unexpected response received", http_res)
|
||||
|
||||
async def complete_async(
|
||||
self,
|
||||
*,
|
||||
messages: Union[
|
||||
List[models.ChatCompletionMessageParam],
|
||||
List[models.ChatCompletionMessageParamTypedDict],
|
||||
],
|
||||
model: Optional[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: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsReasoning,
|
||||
models.ChatCompletionCreateParamsReasoningTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
response_format: Optional[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsResponseFormatUnion,
|
||||
models.ChatCompletionCreateParamsResponseFormatUnionTypedDict,
|
||||
]
|
||||
] = None,
|
||||
seed: OptionalNullable[int] = UNSET,
|
||||
stop: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsStop,
|
||||
models.ChatCompletionCreateParamsStopTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
stream: OptionalNullable[bool] = False,
|
||||
stream_options: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsStreamOptions,
|
||||
models.ChatCompletionCreateParamsStreamOptionsTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
temperature: OptionalNullable[float] = 1,
|
||||
tool_choice: Optional[
|
||||
Union[
|
||||
models.ChatCompletionToolChoiceOption,
|
||||
models.ChatCompletionToolChoiceOptionTypedDict,
|
||||
]
|
||||
] = None,
|
||||
tools: Optional[
|
||||
Union[
|
||||
List[models.ChatCompletionTool],
|
||||
List[models.ChatCompletionToolTypedDict],
|
||||
]
|
||||
] = None,
|
||||
top_p: OptionalNullable[float] = 1,
|
||||
user: Optional[str] = None,
|
||||
models_llm: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[
|
||||
models.ChatCompletionCreateParamsReasoningEffort
|
||||
] = UNSET,
|
||||
provider: OptionalNullable[
|
||||
Union[
|
||||
models.ChatCompletionCreateParamsProvider,
|
||||
models.ChatCompletionCreateParamsProviderTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
plugins: Optional[
|
||||
Union[
|
||||
List[models.ChatCompletionCreateParamsPluginUnion],
|
||||
List[models.ChatCompletionCreateParamsPluginUnionTypedDict],
|
||||
]
|
||||
] = None,
|
||||
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
||||
server_url: Optional[str] = None,
|
||||
timeout_ms: Optional[int] = None,
|
||||
http_headers: Optional[Mapping[str, str]] = None,
|
||||
) -> models.ChatCompletion:
|
||||
r"""Create a chat completion
|
||||
|
||||
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
|
||||
|
||||
:param messages: List of messages for the conversation
|
||||
:param model: Model 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)
|
||||
: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: Reasoning configuration
|
||||
: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:
|
||||
:param temperature: Sampling temperature (0-2)
|
||||
:param tool_choice: Tool choice configuration
|
||||
:param tools: Available tools for function calling
|
||||
:param top_p: Nucleus sampling parameter (0-1)
|
||||
:param user: Unique user identifier
|
||||
:param models_llm: Order of models to fallback to for this request
|
||||
:param reasoning_effort: Reasoning effort
|
||||
: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 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 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 = models.ChatCompletionCreateParams(
|
||||
messages=utils.get_pydantic_model(
|
||||
messages, List[models.ChatCompletionMessageParam]
|
||||
),
|
||||
model=model,
|
||||
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, OptionalNullable[models.ChatCompletionCreateParamsReasoning]
|
||||
),
|
||||
response_format=utils.get_pydantic_model(
|
||||
response_format,
|
||||
Optional[models.ChatCompletionCreateParamsResponseFormatUnion],
|
||||
),
|
||||
seed=seed,
|
||||
stop=stop,
|
||||
stream=stream,
|
||||
stream_options=utils.get_pydantic_model(
|
||||
stream_options,
|
||||
OptionalNullable[models.ChatCompletionCreateParamsStreamOptions],
|
||||
),
|
||||
temperature=temperature,
|
||||
tool_choice=utils.get_pydantic_model(
|
||||
tool_choice, Optional[models.ChatCompletionToolChoiceOption]
|
||||
),
|
||||
tools=utils.get_pydantic_model(
|
||||
tools, Optional[List[models.ChatCompletionTool]]
|
||||
),
|
||||
top_p=top_p,
|
||||
user=user,
|
||||
models_llm=models_llm,
|
||||
reasoning_effort=reasoning_effort,
|
||||
provider=utils.get_pydantic_model(
|
||||
provider, OptionalNullable[models.ChatCompletionCreateParamsProvider]
|
||||
),
|
||||
plugins=utils.get_pydantic_model(
|
||||
plugins, Optional[List[models.ChatCompletionCreateParamsPluginUnion]]
|
||||
),
|
||||
)
|
||||
|
||||
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="application/json",
|
||||
http_headers=http_headers,
|
||||
security=self.sdk_configuration.security,
|
||||
get_serialized_body=lambda: utils.serialize_request_body(
|
||||
request, False, False, "json", models.ChatCompletionCreateParams
|
||||
),
|
||||
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="createChatCompletion",
|
||||
oauth2_scopes=[],
|
||||
security_source=get_security_from_env(
|
||||
self.sdk_configuration.security, models.Security
|
||||
),
|
||||
),
|
||||
request=req,
|
||||
error_status_codes=["400", "401", "429", "4XX", "500", "5XX"],
|
||||
retry_config=retry_config,
|
||||
)
|
||||
|
||||
response_data: Any = None
|
||||
if utils.match_response(http_res, "200", "application/json"):
|
||||
return unmarshal_json_response(models.ChatCompletion, http_res)
|
||||
if utils.match_response(http_res, ["400", "401", "429"], "application/json"):
|
||||
response_data = unmarshal_json_response(
|
||||
errors.ChatCompletionErrorData, http_res
|
||||
)
|
||||
raise errors.ChatCompletionError(response_data, http_res)
|
||||
if utils.match_response(http_res, "500", "application/json"):
|
||||
response_data = unmarshal_json_response(
|
||||
errors.ChatCompletionErrorData, http_res
|
||||
)
|
||||
raise errors.ChatCompletionError(response_data, http_res)
|
||||
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
|
||||
)
|
||||
|
||||
raise errors.OpenRouterDefaultError("Unexpected response received", http_res)
|
||||
|
||||
def complete_stream(
|
||||
self,
|
||||
*,
|
||||
messages: Union[
|
||||
List[models.ChatCompletionMessageParam],
|
||||
List[models.ChatCompletionMessageParamTypedDict],
|
||||
],
|
||||
model: Optional[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: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsReasoning,
|
||||
models.ChatStreamCompletionCreateParamsReasoningTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
response_format: Optional[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsResponseFormatUnion,
|
||||
models.ChatStreamCompletionCreateParamsResponseFormatUnionTypedDict,
|
||||
]
|
||||
] = None,
|
||||
seed: OptionalNullable[int] = UNSET,
|
||||
stop: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsStop,
|
||||
models.ChatStreamCompletionCreateParamsStopTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
stream: Optional[bool] = True,
|
||||
stream_options: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsStreamOptions,
|
||||
models.ChatStreamCompletionCreateParamsStreamOptionsTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
temperature: OptionalNullable[float] = 1,
|
||||
tool_choice: Optional[
|
||||
Union[
|
||||
models.ChatCompletionToolChoiceOption,
|
||||
models.ChatCompletionToolChoiceOptionTypedDict,
|
||||
]
|
||||
] = None,
|
||||
tools: Optional[
|
||||
Union[
|
||||
List[models.ChatCompletionTool],
|
||||
List[models.ChatCompletionToolTypedDict],
|
||||
]
|
||||
] = None,
|
||||
top_p: OptionalNullable[float] = 1,
|
||||
user: Optional[str] = None,
|
||||
models_llm: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[
|
||||
models.ChatStreamCompletionCreateParamsReasoningEffort
|
||||
] = UNSET,
|
||||
provider: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsProvider,
|
||||
models.ChatStreamCompletionCreateParamsProviderTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
plugins: Optional[
|
||||
Union[
|
||||
List[models.ChatStreamCompletionCreateParamsPluginUnion],
|
||||
List[models.ChatStreamCompletionCreateParamsPluginUnionTypedDict],
|
||||
]
|
||||
] = None,
|
||||
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
||||
server_url: Optional[str] = None,
|
||||
timeout_ms: Optional[int] = None,
|
||||
http_headers: Optional[Mapping[str, str]] = None,
|
||||
) -> eventstreaming.EventStream[models.StreamChatCompletionResponseBody]:
|
||||
r"""Create a chat completion
|
||||
|
||||
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
|
||||
|
||||
:param messages: List of messages for the conversation
|
||||
:param model: Model 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)
|
||||
: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: Reasoning configuration
|
||||
: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:
|
||||
:param temperature: Sampling temperature (0-2)
|
||||
:param tool_choice: Tool choice configuration
|
||||
:param tools: Available tools for function calling
|
||||
:param top_p: Nucleus sampling parameter (0-1)
|
||||
:param user: Unique user identifier
|
||||
:param models_llm: Order of models to fallback to for this request
|
||||
:param reasoning_effort: Reasoning effort
|
||||
: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 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 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 = models.ChatStreamCompletionCreateParams(
|
||||
messages=utils.get_pydantic_model(
|
||||
messages, List[models.ChatCompletionMessageParam]
|
||||
),
|
||||
model=model,
|
||||
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,
|
||||
OptionalNullable[models.ChatStreamCompletionCreateParamsReasoning],
|
||||
),
|
||||
response_format=utils.get_pydantic_model(
|
||||
response_format,
|
||||
Optional[models.ChatStreamCompletionCreateParamsResponseFormatUnion],
|
||||
),
|
||||
seed=seed,
|
||||
stop=stop,
|
||||
stream=stream,
|
||||
stream_options=utils.get_pydantic_model(
|
||||
stream_options,
|
||||
OptionalNullable[models.ChatStreamCompletionCreateParamsStreamOptions],
|
||||
),
|
||||
temperature=temperature,
|
||||
tool_choice=utils.get_pydantic_model(
|
||||
tool_choice, Optional[models.ChatCompletionToolChoiceOption]
|
||||
),
|
||||
tools=utils.get_pydantic_model(
|
||||
tools, Optional[List[models.ChatCompletionTool]]
|
||||
),
|
||||
top_p=top_p,
|
||||
user=user,
|
||||
models_llm=models_llm,
|
||||
reasoning_effort=reasoning_effort,
|
||||
provider=utils.get_pydantic_model(
|
||||
provider,
|
||||
OptionalNullable[models.ChatStreamCompletionCreateParamsProvider],
|
||||
),
|
||||
plugins=utils.get_pydantic_model(
|
||||
plugins,
|
||||
Optional[List[models.ChatStreamCompletionCreateParamsPluginUnion]],
|
||||
),
|
||||
)
|
||||
|
||||
req = self._build_request(
|
||||
method="POST",
|
||||
path="/chat/completions#stream",
|
||||
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",
|
||||
http_headers=http_headers,
|
||||
security=self.sdk_configuration.security,
|
||||
get_serialized_body=lambda: utils.serialize_request_body(
|
||||
request, False, False, "json", models.ChatStreamCompletionCreateParams
|
||||
),
|
||||
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="streamChatCompletion",
|
||||
oauth2_scopes=[],
|
||||
security_source=get_security_from_env(
|
||||
self.sdk_configuration.security, models.Security
|
||||
),
|
||||
),
|
||||
request=req,
|
||||
error_status_codes=["400", "401", "429", "4XX", "500", "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(
|
||||
models.ChatCompletion, 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, models.StreamChatCompletionResponseBody
|
||||
raw, models.ChatCompletionChunkWrapper
|
||||
),
|
||||
sentinel="[DONE]",
|
||||
client_ref=self,
|
||||
)
|
||||
if utils.match_response(http_res, ["400", "401", "429"], "application/json"):
|
||||
http_res_text = utils.stream_to_text(http_res)
|
||||
@@ -735,7 +356,7 @@ class Chat(BaseSDK):
|
||||
"Unexpected response received", http_res, http_res_text
|
||||
)
|
||||
|
||||
async def complete_stream_async(
|
||||
async def complete_async(
|
||||
self,
|
||||
*,
|
||||
messages: Union[
|
||||
@@ -752,30 +373,16 @@ class Chat(BaseSDK):
|
||||
metadata: Optional[Dict[str, str]] = None,
|
||||
presence_penalty: OptionalNullable[float] = UNSET,
|
||||
reasoning: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsReasoning,
|
||||
models.ChatStreamCompletionCreateParamsReasoningTypedDict,
|
||||
]
|
||||
Union[models.Reasoning, models.ReasoningTypedDict]
|
||||
] = UNSET,
|
||||
response_format: Optional[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsResponseFormatUnion,
|
||||
models.ChatStreamCompletionCreateParamsResponseFormatUnionTypedDict,
|
||||
]
|
||||
Union[models.ResponseFormat, models.ResponseFormatTypedDict]
|
||||
] = None,
|
||||
seed: OptionalNullable[int] = UNSET,
|
||||
stop: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsStop,
|
||||
models.ChatStreamCompletionCreateParamsStopTypedDict,
|
||||
]
|
||||
] = UNSET,
|
||||
stream: Optional[bool] = True,
|
||||
stop: OptionalNullable[Union[models.Stop, models.StopTypedDict]] = UNSET,
|
||||
stream: OptionalNullable[bool] = False,
|
||||
stream_options: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsStreamOptions,
|
||||
models.ChatStreamCompletionCreateParamsStreamOptionsTypedDict,
|
||||
]
|
||||
Union[models.StreamOptions, models.StreamOptionsTypedDict]
|
||||
] = UNSET,
|
||||
temperature: OptionalNullable[float] = 1,
|
||||
tool_choice: Optional[
|
||||
@@ -792,27 +399,20 @@ class Chat(BaseSDK):
|
||||
] = None,
|
||||
top_p: OptionalNullable[float] = 1,
|
||||
user: Optional[str] = None,
|
||||
models_llm: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[
|
||||
models.ChatStreamCompletionCreateParamsReasoningEffort
|
||||
] = UNSET,
|
||||
model_list: OptionalNullable[List[str]] = UNSET,
|
||||
reasoning_effort: OptionalNullable[models.ReasoningEffort] = UNSET,
|
||||
provider: OptionalNullable[
|
||||
Union[
|
||||
models.ChatStreamCompletionCreateParamsProvider,
|
||||
models.ChatStreamCompletionCreateParamsProviderTypedDict,
|
||||
]
|
||||
Union[models.Provider, models.ProviderTypedDict]
|
||||
] = UNSET,
|
||||
plugins: Optional[
|
||||
Union[
|
||||
List[models.ChatStreamCompletionCreateParamsPluginUnion],
|
||||
List[models.ChatStreamCompletionCreateParamsPluginUnionTypedDict],
|
||||
]
|
||||
Union[List[models.Plugin], List[models.PluginTypedDict]]
|
||||
] = None,
|
||||
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
||||
server_url: Optional[str] = None,
|
||||
timeout_ms: Optional[int] = None,
|
||||
accept_header_override: Optional[CompleteAcceptEnum] = None,
|
||||
http_headers: Optional[Mapping[str, str]] = None,
|
||||
) -> eventstreaming.EventStreamAsync[models.StreamChatCompletionResponseBody]:
|
||||
) -> models.CreateChatCompletionResponse:
|
||||
r"""Create a chat completion
|
||||
|
||||
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
|
||||
@@ -838,13 +438,14 @@ class Chat(BaseSDK):
|
||||
:param tools: Available tools for function calling
|
||||
:param top_p: Nucleus sampling parameter (0-1)
|
||||
:param user: Unique user identifier
|
||||
:param models_llm: Order of models to fallback to for this request
|
||||
:param model_list: Order of models to fallback to for this request
|
||||
:param reasoning_effort: Reasoning effort
|
||||
: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 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
|
||||
@@ -857,7 +458,7 @@ class Chat(BaseSDK):
|
||||
else:
|
||||
base_url = self._get_url(base_url, url_variables)
|
||||
|
||||
request = models.ChatStreamCompletionCreateParams(
|
||||
request = models.ChatCompletionCreateParams(
|
||||
messages=utils.get_pydantic_model(
|
||||
messages, List[models.ChatCompletionMessageParam]
|
||||
),
|
||||
@@ -871,19 +472,16 @@ class Chat(BaseSDK):
|
||||
metadata=metadata,
|
||||
presence_penalty=presence_penalty,
|
||||
reasoning=utils.get_pydantic_model(
|
||||
reasoning,
|
||||
OptionalNullable[models.ChatStreamCompletionCreateParamsReasoning],
|
||||
reasoning, OptionalNullable[models.Reasoning]
|
||||
),
|
||||
response_format=utils.get_pydantic_model(
|
||||
response_format,
|
||||
Optional[models.ChatStreamCompletionCreateParamsResponseFormatUnion],
|
||||
response_format, Optional[models.ResponseFormat]
|
||||
),
|
||||
seed=seed,
|
||||
stop=stop,
|
||||
stream=stream,
|
||||
stream_options=utils.get_pydantic_model(
|
||||
stream_options,
|
||||
OptionalNullable[models.ChatStreamCompletionCreateParamsStreamOptions],
|
||||
stream_options, OptionalNullable[models.StreamOptions]
|
||||
),
|
||||
temperature=temperature,
|
||||
tool_choice=utils.get_pydantic_model(
|
||||
@@ -894,21 +492,17 @@ class Chat(BaseSDK):
|
||||
),
|
||||
top_p=top_p,
|
||||
user=user,
|
||||
models_llm=models_llm,
|
||||
model_list=model_list,
|
||||
reasoning_effort=reasoning_effort,
|
||||
provider=utils.get_pydantic_model(
|
||||
provider,
|
||||
OptionalNullable[models.ChatStreamCompletionCreateParamsProvider],
|
||||
),
|
||||
plugins=utils.get_pydantic_model(
|
||||
plugins,
|
||||
Optional[List[models.ChatStreamCompletionCreateParamsPluginUnion]],
|
||||
provider, OptionalNullable[models.Provider]
|
||||
),
|
||||
plugins=utils.get_pydantic_model(plugins, Optional[List[models.Plugin]]),
|
||||
)
|
||||
|
||||
req = self._build_request_async(
|
||||
method="POST",
|
||||
path="/chat/completions#stream",
|
||||
path="/chat/completions",
|
||||
base_url=base_url,
|
||||
url_variables=url_variables,
|
||||
request=request,
|
||||
@@ -916,11 +510,13 @@ class Chat(BaseSDK):
|
||||
request_has_path_params=False,
|
||||
request_has_query_params=True,
|
||||
user_agent_header="user-agent",
|
||||
accept_header_value="text/event-stream",
|
||||
accept_header_value=accept_header_override.value
|
||||
if accept_header_override is not None
|
||||
else "application/json;q=1, text/event-stream;q=0",
|
||||
http_headers=http_headers,
|
||||
security=self.sdk_configuration.security,
|
||||
get_serialized_body=lambda: utils.serialize_request_body(
|
||||
request, False, False, "json", models.ChatStreamCompletionCreateParams
|
||||
request, False, False, "json", models.ChatCompletionCreateParams
|
||||
),
|
||||
timeout_ms=timeout_ms,
|
||||
)
|
||||
@@ -937,7 +533,7 @@ class Chat(BaseSDK):
|
||||
hook_ctx=HookContext(
|
||||
config=self.sdk_configuration,
|
||||
base_url=base_url or "",
|
||||
operation_id="streamChatCompletion",
|
||||
operation_id="createChatCompletion",
|
||||
oauth2_scopes=[],
|
||||
security_source=get_security_from_env(
|
||||
self.sdk_configuration.security, models.Security
|
||||
@@ -950,13 +546,19 @@ class Chat(BaseSDK):
|
||||
)
|
||||
|
||||
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(
|
||||
models.ChatCompletion, 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, models.StreamChatCompletionResponseBody
|
||||
raw, models.ChatCompletionChunkWrapper
|
||||
),
|
||||
sentinel="[DONE]",
|
||||
client_ref=self,
|
||||
)
|
||||
if utils.match_response(http_res, ["400", "401", "429"], "application/json"):
|
||||
http_res_text = await utils.stream_to_text_async(http_res)
|
||||
|
||||
Reference in New Issue
Block a user