Skip to content

Commit 0bb61c7

Browse files
committed
feat: Add service_tier paramater to ChatOpenAI()
1 parent c006481 commit 0bb61c7

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

chatlas/_provider_openai.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def ChatOpenAI(
4949
model: "Optional[ResponsesModel | str]" = None,
5050
api_key: Optional[str] = None,
5151
base_url: str = "https://api.openai.com/v1",
52+
service_tier: Optional[
53+
Literal["auto", "default", "flex", "scale", "priority"]
54+
] = None,
5255
kwargs: Optional["ChatClientArgs"] = None,
5356
) -> Chat["SubmitInputArgs", Response]:
5457
"""
@@ -93,6 +96,13 @@ def ChatOpenAI(
9396
variable.
9497
base_url
9598
The base URL to the endpoint; the default uses OpenAI.
99+
service_tier
100+
Request a specific service tier. Options:
101+
- `"auto"` (default): uses the service tier configured in Project settings.
102+
- `"default"`: standard pricing and performance.
103+
- `"flex"`: slower and cheaper.
104+
- `"scale"`: batch-like pricing for high-volume use.
105+
- `"priority"`: faster and more expensive.
96106
kwargs
97107
Additional arguments to pass to the `openai.OpenAI()` client
98108
constructor.
@@ -146,6 +156,10 @@ def ChatOpenAI(
146156
if model is None:
147157
model = log_model_default("gpt-4.1")
148158

159+
kwargs_chat: "SubmitInputArgs" = {}
160+
if service_tier is not None:
161+
kwargs_chat["service_tier"] = service_tier
162+
149163
return Chat(
150164
provider=OpenAIProvider(
151165
api_key=api_key,
@@ -154,6 +168,7 @@ def ChatOpenAI(
154168
kwargs=kwargs,
155169
),
156170
system_prompt=system_prompt,
171+
kwargs_chat=kwargs_chat,
157172
)
158173

159174

chatlas/_provider_openai_azure.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Optional
3+
from typing import TYPE_CHECKING, Literal, Optional
44

55
from openai import AsyncAzureOpenAI, AzureOpenAI
66
from openai.types.chat import ChatCompletion
@@ -21,6 +21,9 @@ def ChatAzureOpenAI(
2121
api_version: str,
2222
api_key: Optional[str] = None,
2323
system_prompt: Optional[str] = None,
24+
service_tier: Optional[
25+
Literal["auto", "default", "flex", "scale", "priority"]
26+
] = None,
2427
kwargs: Optional["ChatAzureClientArgs"] = None,
2528
) -> Chat["SubmitInputArgs", ChatCompletion]:
2629
"""
@@ -62,6 +65,13 @@ def ChatAzureOpenAI(
6265
variable.
6366
system_prompt
6467
A system prompt to set the behavior of the assistant.
68+
service_tier
69+
Request a specific service tier. Options:
70+
- `"auto"` (default): uses the service tier configured in Project settings.
71+
- `"default"`: standard pricing and performance.
72+
- `"flex"`: slower and cheaper.
73+
- `"scale"`: batch-like pricing for high-volume use.
74+
- `"priority"`: faster and more expensive.
6575
kwargs
6676
Additional arguments to pass to the `openai.AzureOpenAI()` client constructor.
6777
@@ -71,6 +81,10 @@ def ChatAzureOpenAI(
7181
A Chat object.
7282
"""
7383

84+
kwargs_chat: "SubmitInputArgs" = {}
85+
if service_tier is not None:
86+
kwargs_chat["service_tier"] = service_tier
87+
7488
return Chat(
7589
provider=OpenAIAzureProvider(
7690
endpoint=endpoint,
@@ -80,6 +94,7 @@ def ChatAzureOpenAI(
8094
kwargs=kwargs,
8195
),
8296
system_prompt=system_prompt,
97+
kwargs_chat=kwargs_chat,
8398
)
8499

85100

tests/test_provider_openai.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,32 @@ def test_openai_custom_http_client():
105105

106106
def test_openai_list_models():
107107
assert_list_models(ChatOpenAI)
108+
109+
110+
def test_openai_service_tier():
111+
"""Test that service_tier parameter is properly passed through."""
112+
# Test that we can create chat instances with different service tiers
113+
# and that the parameter is stored correctly
114+
chat_auto = ChatOpenAI(
115+
system_prompt="Be as terse as possible; no punctuation",
116+
service_tier="auto",
117+
)
118+
assert chat_auto.provider._service_tier == "auto"
119+
120+
chat_flex = ChatOpenAI(
121+
system_prompt="Be as terse as possible; no punctuation",
122+
service_tier="flex",
123+
)
124+
assert chat_flex.provider._service_tier == "flex"
125+
126+
chat_default = ChatOpenAI(
127+
system_prompt="Be as terse as possible; no punctuation",
128+
service_tier="default",
129+
)
130+
assert chat_default.provider._service_tier == "default"
131+
132+
# Test that auto is the default
133+
chat_default_implicit = ChatOpenAI(
134+
system_prompt="Be as terse as possible; no punctuation",
135+
)
136+
assert chat_default_implicit.provider._service_tier == "auto"

0 commit comments

Comments
 (0)