This commit is contained in:
David Alberto Adler
2025-09-07 21:44:36 +01:00
parent f7f8677367
commit ed17d25183
17 changed files with 66 additions and 260 deletions
+2 -2
View File
@@ -3,10 +3,10 @@
import importlib.metadata
__title__: str = "openrouter"
__version__: str = "0.4.1"
__version__: str = "0.5.1"
__openapi_doc_version__: str = "1.0.0"
__gen_version__: str = "2.694.1"
__user_agent__: str = "speakeasy-sdk/python 0.4.1 2.694.1 1.0.0 openrouter"
__user_agent__: str = "speakeasy-sdk/python 0.5.1 2.694.1 1.0.0 openrouter"
try:
if __package__ is not None:
+16 -71
View File
@@ -1,7 +1,6 @@
"""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
@@ -9,16 +8,6 @@ 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"""
@@ -99,9 +88,8 @@ class Chat(BaseSDK):
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.CreateChatCompletionResponse:
) -> models.ChatCompletion:
r"""Create a chat completion
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
@@ -134,7 +122,6 @@ class Chat(BaseSDK):
: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
@@ -203,9 +190,7 @@ class Chat(BaseSDK):
request_has_path_params=False,
request_has_query_params=True,
user_agent_header="user-agent",
accept_header_value=accept_header_override.value
if accept_header_override is not None
else "application/json;q=1, text/event-stream;q=0",
accept_header_value="application/json",
http_headers=http_headers,
security=self.sdk_configuration.security,
get_serialized_body=lambda: utils.serialize_request_body(
@@ -234,37 +219,22 @@ class Chat(BaseSDK):
),
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.ChatCompletionChunkWrapper
),
sentinel="[DONE]",
client_ref=self,
)
return unmarshal_json_response(models.ChatCompletion, http_res)
if utils.match_response(http_res, ["400", "401", "429"], "application/json"):
http_res_text = utils.stream_to_text(http_res)
response_data = unmarshal_json_response(
errors.ChatCompletionErrorData, http_res, http_res_text
errors.ChatCompletionErrorData, http_res
)
raise errors.ChatCompletionError(response_data, http_res, http_res_text)
raise errors.ChatCompletionError(response_data, http_res)
if utils.match_response(http_res, "500", "application/json"):
http_res_text = utils.stream_to_text(http_res)
response_data = unmarshal_json_response(
errors.ChatCompletionErrorData, http_res, http_res_text
errors.ChatCompletionErrorData, http_res
)
raise errors.ChatCompletionError(response_data, http_res, http_res_text)
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(
@@ -276,10 +246,7 @@ class Chat(BaseSDK):
"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
)
raise errors.OpenRouterDefaultError("Unexpected response received", http_res)
async def complete_async(
self,
@@ -357,9 +324,8 @@ class Chat(BaseSDK):
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.CreateChatCompletionResponse:
) -> models.ChatCompletion:
r"""Create a chat completion
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
@@ -392,7 +358,6 @@ class Chat(BaseSDK):
: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
@@ -461,9 +426,7 @@ class Chat(BaseSDK):
request_has_path_params=False,
request_has_query_params=True,
user_agent_header="user-agent",
accept_header_value=accept_header_override.value
if accept_header_override is not None
else "application/json;q=1, text/event-stream;q=0",
accept_header_value="application/json",
http_headers=http_headers,
security=self.sdk_configuration.security,
get_serialized_body=lambda: utils.serialize_request_body(
@@ -492,37 +455,22 @@ class Chat(BaseSDK):
),
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 = 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.ChatCompletionChunkWrapper
),
sentinel="[DONE]",
client_ref=self,
)
return unmarshal_json_response(models.ChatCompletion, http_res)
if utils.match_response(http_res, ["400", "401", "429"], "application/json"):
http_res_text = await utils.stream_to_text_async(http_res)
response_data = unmarshal_json_response(
errors.ChatCompletionErrorData, http_res, http_res_text
errors.ChatCompletionErrorData, http_res
)
raise errors.ChatCompletionError(response_data, http_res, http_res_text)
raise errors.ChatCompletionError(response_data, http_res)
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.ChatCompletionErrorData, http_res, http_res_text
errors.ChatCompletionErrorData, http_res
)
raise errors.ChatCompletionError(response_data, http_res, http_res_text)
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(
@@ -534,10 +482,7 @@ class Chat(BaseSDK):
"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
)
raise errors.OpenRouterDefaultError("Unexpected response received", http_res)
def complete_stream(
self,
-16
View File
@@ -46,10 +46,6 @@ if TYPE_CHECKING:
ChatCompletionChunkChoiceDeltaToolCallType,
ChatCompletionChunkChoiceDeltaToolCallTypedDict,
)
from .chatcompletionchunkwrapper import (
ChatCompletionChunkWrapper,
ChatCompletionChunkWrapperTypedDict,
)
from .chatcompletioncontentpart import (
ChatCompletionContentPart,
ChatCompletionContentPartTypedDict,
@@ -304,10 +300,6 @@ if TYPE_CHECKING:
PromptTokensDetails,
PromptTokensDetailsTypedDict,
)
from .createchatcompletionop import (
CreateChatCompletionResponse,
CreateChatCompletionResponseTypedDict,
)
from .fileannotationdetail import (
ContentImageURL,
ContentImageURLTypedDict,
@@ -387,8 +379,6 @@ __all__ = [
"ChatCompletionChunkChoiceTypedDict",
"ChatCompletionChunkObject",
"ChatCompletionChunkTypedDict",
"ChatCompletionChunkWrapper",
"ChatCompletionChunkWrapperTypedDict",
"ChatCompletionContentPart",
"ChatCompletionContentPartAudio",
"ChatCompletionContentPartAudioFormat",
@@ -605,8 +595,6 @@ __all__ = [
"ContentTextTypedDict",
"ContentTypeImageURL",
"ContentTypeText",
"CreateChatCompletionResponse",
"CreateChatCompletionResponseTypedDict",
"Detail",
"Error",
"ErrorTypedDict",
@@ -682,8 +670,6 @@ _dynamic_imports: dict[str, str] = {
"ChatCompletionChunkChoiceDeltaToolCallFunctionTypedDict": ".chatcompletionchunkchoicedeltatoolcall",
"ChatCompletionChunkChoiceDeltaToolCallType": ".chatcompletionchunkchoicedeltatoolcall",
"ChatCompletionChunkChoiceDeltaToolCallTypedDict": ".chatcompletionchunkchoicedeltatoolcall",
"ChatCompletionChunkWrapper": ".chatcompletionchunkwrapper",
"ChatCompletionChunkWrapperTypedDict": ".chatcompletionchunkwrapper",
"ChatCompletionContentPart": ".chatcompletioncontentpart",
"ChatCompletionContentPartTypedDict": ".chatcompletioncontentpart",
"ChatCompletionContentPartAudio": ".chatcompletioncontentpartaudio",
@@ -903,8 +889,6 @@ _dynamic_imports: dict[str, str] = {
"CompletionUsageTypedDict": ".completionusage",
"PromptTokensDetails": ".completionusage",
"PromptTokensDetailsTypedDict": ".completionusage",
"CreateChatCompletionResponse": ".createchatcompletionop",
"CreateChatCompletionResponseTypedDict": ".createchatcompletionop",
"ContentImageURL": ".fileannotationdetail",
"ContentImageURLTypedDict": ".fileannotationdetail",
"ContentText": ".fileannotationdetail",
@@ -1,16 +0,0 @@
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
from __future__ import annotations
from .chatcompletionchunk import ChatCompletionChunk, ChatCompletionChunkTypedDict
from openrouter.types import BaseModel
from typing_extensions import TypedDict
class ChatCompletionChunkWrapperTypedDict(TypedDict):
data: ChatCompletionChunkTypedDict
r"""Streaming chat completion chunk"""
class ChatCompletionChunkWrapper(BaseModel):
data: ChatCompletionChunk
r"""Streaming chat completion chunk"""
@@ -1,35 +0,0 @@
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
from __future__ import annotations
from .chatcompletion import ChatCompletion, ChatCompletionTypedDict
from .chatcompletionchunkwrapper import (
ChatCompletionChunkWrapper,
ChatCompletionChunkWrapperTypedDict,
)
from openrouter.utils import eventstreaming
from typing import Union
from typing_extensions import TypeAliasType
CreateChatCompletionResponseTypedDict = TypeAliasType(
"CreateChatCompletionResponseTypedDict",
Union[
ChatCompletionTypedDict,
Union[
eventstreaming.EventStream[ChatCompletionChunkWrapperTypedDict],
eventstreaming.EventStreamAsync[ChatCompletionChunkWrapperTypedDict],
],
],
)
CreateChatCompletionResponse = TypeAliasType(
"CreateChatCompletionResponse",
Union[
ChatCompletion,
Union[
eventstreaming.EventStream[ChatCompletionChunkWrapper],
eventstreaming.EventStreamAsync[ChatCompletionChunkWrapper],
],
],
)