mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-07-31 12:30:30 +08:00
15 KiB
15 KiB
Chat
(chat)
Overview
Available Operations
- complete - 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
import os
with OpenRouter(
api_key=os.getenv("OPENROUTER_API_KEY", ""),
) as open_router:
res = open_router.chat.complete(messages=[
{
"role": "user",
"content": "Hello, how are you?",
},
], stream=False, 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.Reasoning] | ➖ | Reasoning configuration | |
response_format |
Optional[models.ResponseFormat] | ➖ | Response format configuration | |
seed |
OptionalNullable[int] | ➖ | Random seed for deterministic outputs | |
stop |
OptionalNullable[models.Stop] | ➖ | Stop sequences (up to 4) | |
stream |
OptionalNullable[bool] | ➖ | Enable streaming response | |
stream_options |
OptionalNullable[models.StreamOptions] | ➖ | 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 | |
model_list |
List[str] | ➖ | Order of models to fallback to for this request | |
reasoning_effort |
OptionalNullable[models.ReasoningEffort] | ➖ | Reasoning effort | |
provider |
OptionalNullable[models.Provider] | ➖ | When multiple model providers are available, optionally indicate your routing preference. | |
plugins |
List[models.Plugin] | ➖ | 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
models.CreateChatCompletionResponse
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.ChatCompletionError | 400, 401, 429 | application/json |
| errors.ChatCompletionError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |