mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-07-31 12:30:30 +08:00
Chat
(chat)
Overview
Chat completion operations
Available Operations
- complete - Create a chat completion
- complete_stream - Create a chat completion
complete
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
Example Usage
from openrouter import OpenRouter, models
import os
with OpenRouter(
bearer_auth=os.getenv("OPENROUTER_BEARER_AUTH", ""),
) as open_router:
res = open_router.chat.complete(messages=[
{
"role": models.ChatCompletionUserMessageParamRole.USER,
"content": "Hello, how are you?",
},
], stream=False, temperature=1, top_p=1)
# Handle response
print(res)
Parameters
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
messages |
List[models.ChatCompletionMessageParam] | ✔️ | List of messages for the conversation | [ { "role": "user", "content": "Hello, how are you?" } ] |
model |
Optional[str] | ➖ | Model to use for completion | |
frequency_penalty |
OptionalNullable[float] | ➖ | Frequency penalty (-2.0 to 2.0) | |
logit_bias |
Dict[str, float] | ➖ | Token logit bias adjustments | |
logprobs |
OptionalNullable[bool] | ➖ | Return log probabilities | |
top_logprobs |
OptionalNullable[float] | ➖ | Number of top log probabilities to return (0-20) | |
max_completion_tokens |
OptionalNullable[float] | ➖ | Maximum tokens in completion | |
max_tokens |
OptionalNullable[float] | ➖ | Maximum tokens (deprecated, use max_completion_tokens) | |
metadata |
Dict[str, str] | ➖ | Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values) | |
presence_penalty |
OptionalNullable[float] | ➖ | Presence penalty (-2.0 to 2.0) | |
reasoning |
OptionalNullable[models.ChatCompletionCreateParamsReasoning] | ➖ | Reasoning configuration | |
response_format |
Optional[models.ChatCompletionCreateParamsResponseFormatUnion] | ➖ | Response format configuration | |
seed |
OptionalNullable[int] | ➖ | Random seed for deterministic outputs | |
stop |
OptionalNullable[models.ChatCompletionCreateParamsStop] | ➖ | Stop sequences (up to 4) | |
stream |
OptionalNullable[bool] | ➖ | Enable streaming response | |
stream_options |
OptionalNullable[models.ChatCompletionCreateParamsStreamOptions] | ➖ | N/A | |
temperature |
OptionalNullable[float] | ➖ | Sampling temperature (0-2) | |
tool_choice |
Optional[models.ChatCompletionToolChoiceOption] | ➖ | Tool choice configuration | |
tools |
List[models.ChatCompletionTool] | ➖ | Available tools for function calling | |
top_p |
OptionalNullable[float] | ➖ | Nucleus sampling parameter (0-1) | |
user |
Optional[str] | ➖ | Unique user identifier | |
models_llm |
List[str] | ➖ | Order of models to fallback to for this request | |
reasoning_effort |
OptionalNullable[models.ChatCompletionCreateParamsReasoningEffort] | ➖ | Reasoning effort | |
provider |
OptionalNullable[models.ChatCompletionCreateParamsProvider] | ➖ | When multiple model providers are available, optionally indicate your routing preference. | |
plugins |
List[models.ChatCompletionCreateParamsPluginUnion] | ➖ | Plugins you want to enable for this request, including their settings. | |
retries |
Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.ChatCompletionError | 400, 401, 429 | application/json |
| errors.ChatCompletionError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
complete_stream
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
Example Usage
from openrouter import OpenRouter, models
import os
with OpenRouter(
bearer_auth=os.getenv("OPENROUTER_BEARER_AUTH", ""),
) as open_router:
res = open_router.chat.complete_stream(messages=[
{
"role": models.ChatCompletionUserMessageParamRole.USER,
"content": "Hello, how are you?",
},
], stream=True, temperature=1, top_p=1)
with res as event_stream:
for event in event_stream:
# handle event
print(event, flush=True)
Parameters
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
messages |
List[models.ChatCompletionMessageParam] | ✔️ | List of messages for the conversation | [ { "role": "user", "content": "Hello, how are you?" } ] |
model |
Optional[str] | ➖ | Model to use for completion | |
frequency_penalty |
OptionalNullable[float] | ➖ | Frequency penalty (-2.0 to 2.0) | |
logit_bias |
Dict[str, float] | ➖ | Token logit bias adjustments | |
logprobs |
OptionalNullable[bool] | ➖ | Return log probabilities | |
top_logprobs |
OptionalNullable[float] | ➖ | Number of top log probabilities to return (0-20) | |
max_completion_tokens |
OptionalNullable[float] | ➖ | Maximum tokens in completion | |
max_tokens |
OptionalNullable[float] | ➖ | Maximum tokens (deprecated, use max_completion_tokens) | |
metadata |
Dict[str, str] | ➖ | Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values) | |
presence_penalty |
OptionalNullable[float] | ➖ | Presence penalty (-2.0 to 2.0) | |
reasoning |
OptionalNullable[models.ChatStreamCompletionCreateParamsReasoning] | ➖ | Reasoning configuration | |
response_format |
Optional[models.ChatStreamCompletionCreateParamsResponseFormatUnion] | ➖ | Response format configuration | |
seed |
OptionalNullable[int] | ➖ | Random seed for deterministic outputs | |
stop |
OptionalNullable[models.ChatStreamCompletionCreateParamsStop] | ➖ | Stop sequences (up to 4) | |
stream |
Optional[bool] | ➖ | Enable streaming response | |
stream_options |
OptionalNullable[models.ChatStreamCompletionCreateParamsStreamOptions] | ➖ | N/A | |
temperature |
OptionalNullable[float] | ➖ | Sampling temperature (0-2) | |
tool_choice |
Optional[models.ChatCompletionToolChoiceOption] | ➖ | Tool choice configuration | |
tools |
List[models.ChatCompletionTool] | ➖ | Available tools for function calling | |
top_p |
OptionalNullable[float] | ➖ | Nucleus sampling parameter (0-1) | |
user |
Optional[str] | ➖ | Unique user identifier | |
models_llm |
List[str] | ➖ | Order of models to fallback to for this request | |
reasoning_effort |
OptionalNullable[models.ChatStreamCompletionCreateParamsReasoningEffort] | ➖ | Reasoning effort | |
provider |
OptionalNullable[models.ChatStreamCompletionCreateParamsProvider] | ➖ | When multiple model providers are available, optionally indicate your routing preference. | |
plugins |
List[models.ChatStreamCompletionCreateParamsPluginUnion] | ➖ | Plugins you want to enable for this request, including their settings. | |
retries |
Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.ChatCompletionError | 400, 401, 429 | application/json |
| errors.ChatCompletionError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |