Commit 6cc51587
Changed files (11)
src
tests
api_resources
src/openai/resources/chat/completions.py
@@ -27,6 +27,7 @@ from ...types.chat.chat_completion import ChatCompletion
from ...types.chat.chat_completion_chunk import ChatCompletionChunk
from ...types.chat.chat_completion_tool_param import ChatCompletionToolParam
from ...types.chat.chat_completion_message_param import ChatCompletionMessageParam
+from ...types.chat.chat_completion_stream_options_param import ChatCompletionStreamOptionsParam
from ...types.chat.chat_completion_tool_choice_option_param import ChatCompletionToolChoiceOptionParam
__all__ = ["Completions", "AsyncCompletions"]
@@ -59,6 +60,7 @@ class Completions(SyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -165,6 +167,8 @@ class Completions(SyncAPIResource):
message.
[Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
make the output more random, while lower values like 0.2 will make it more
focused and deterministic.
@@ -227,6 +231,7 @@ class Completions(SyncAPIResource):
response_format: completion_create_params.ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -333,6 +338,8 @@ class Completions(SyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
make the output more random, while lower values like 0.2 will make it more
focused and deterministic.
@@ -395,6 +402,7 @@ class Completions(SyncAPIResource):
response_format: completion_create_params.ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -501,6 +509,8 @@ class Completions(SyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
make the output more random, while lower values like 0.2 will make it more
focused and deterministic.
@@ -563,6 +573,7 @@ class Completions(SyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -594,6 +605,7 @@ class Completions(SyncAPIResource):
"seed": seed,
"stop": stop,
"stream": stream,
+ "stream_options": stream_options,
"temperature": temperature,
"tool_choice": tool_choice,
"tools": tools,
@@ -639,6 +651,7 @@ class AsyncCompletions(AsyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -745,6 +758,8 @@ class AsyncCompletions(AsyncAPIResource):
message.
[Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
make the output more random, while lower values like 0.2 will make it more
focused and deterministic.
@@ -807,6 +822,7 @@ class AsyncCompletions(AsyncAPIResource):
response_format: completion_create_params.ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -913,6 +929,8 @@ class AsyncCompletions(AsyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
make the output more random, while lower values like 0.2 will make it more
focused and deterministic.
@@ -975,6 +993,7 @@ class AsyncCompletions(AsyncAPIResource):
response_format: completion_create_params.ResponseFormat | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -1081,6 +1100,8 @@ class AsyncCompletions(AsyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
make the output more random, while lower values like 0.2 will make it more
focused and deterministic.
@@ -1143,6 +1164,7 @@ class AsyncCompletions(AsyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str]] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven = NOT_GIVEN,
tools: Iterable[ChatCompletionToolParam] | NotGiven = NOT_GIVEN,
@@ -1174,6 +1196,7 @@ class AsyncCompletions(AsyncAPIResource):
"seed": seed,
"stop": stop,
"stream": stream,
+ "stream_options": stream_options,
"temperature": temperature,
"tool_choice": tool_choice,
"tools": tools,
src/openai/resources/completions.py
@@ -23,6 +23,7 @@ from .._base_client import (
make_request_options,
)
from ..types.completion import Completion
+from ..types.chat.chat_completion_stream_options_param import ChatCompletionStreamOptionsParam
__all__ = ["Completions", "AsyncCompletions"]
@@ -53,6 +54,7 @@ class Completions(SyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -156,6 +158,8 @@ class Completions(SyncAPIResource):
message.
[Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
suffix: The suffix that comes after a completion of inserted text.
This parameter is only supported for `gpt-3.5-turbo-instruct`.
@@ -203,6 +207,7 @@ class Completions(SyncAPIResource):
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -306,6 +311,8 @@ class Completions(SyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens. The
returned text will not contain the stop sequence.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
suffix: The suffix that comes after a completion of inserted text.
This parameter is only supported for `gpt-3.5-turbo-instruct`.
@@ -353,6 +360,7 @@ class Completions(SyncAPIResource):
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -456,6 +464,8 @@ class Completions(SyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens. The
returned text will not contain the stop sequence.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
suffix: The suffix that comes after a completion of inserted text.
This parameter is only supported for `gpt-3.5-turbo-instruct`.
@@ -503,6 +513,7 @@ class Completions(SyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -531,6 +542,7 @@ class Completions(SyncAPIResource):
"seed": seed,
"stop": stop,
"stream": stream,
+ "stream_options": stream_options,
"suffix": suffix,
"temperature": temperature,
"top_p": top_p,
@@ -573,6 +585,7 @@ class AsyncCompletions(AsyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -676,6 +689,8 @@ class AsyncCompletions(AsyncAPIResource):
message.
[Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
suffix: The suffix that comes after a completion of inserted text.
This parameter is only supported for `gpt-3.5-turbo-instruct`.
@@ -723,6 +738,7 @@ class AsyncCompletions(AsyncAPIResource):
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -826,6 +842,8 @@ class AsyncCompletions(AsyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens. The
returned text will not contain the stop sequence.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
suffix: The suffix that comes after a completion of inserted text.
This parameter is only supported for `gpt-3.5-turbo-instruct`.
@@ -873,6 +891,7 @@ class AsyncCompletions(AsyncAPIResource):
presence_penalty: Optional[float] | NotGiven = NOT_GIVEN,
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -976,6 +995,8 @@ class AsyncCompletions(AsyncAPIResource):
stop: Up to 4 sequences where the API will stop generating further tokens. The
returned text will not contain the stop sequence.
+ stream_options: Options for streaming response. Only set this when you set `stream: true`.
+
suffix: The suffix that comes after a completion of inserted text.
This parameter is only supported for `gpt-3.5-turbo-instruct`.
@@ -1023,6 +1044,7 @@ class AsyncCompletions(AsyncAPIResource):
seed: Optional[int] | NotGiven = NOT_GIVEN,
stop: Union[Optional[str], List[str], None] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
+ stream_options: Optional[ChatCompletionStreamOptionsParam] | NotGiven = NOT_GIVEN,
suffix: Optional[str] | NotGiven = NOT_GIVEN,
temperature: Optional[float] | NotGiven = NOT_GIVEN,
top_p: Optional[float] | NotGiven = NOT_GIVEN,
@@ -1051,6 +1073,7 @@ class AsyncCompletions(AsyncAPIResource):
"seed": seed,
"stop": stop,
"stream": stream,
+ "stream_options": stream_options,
"suffix": suffix,
"temperature": temperature,
"top_p": top_p,
src/openai/types/chat/__init__.py
@@ -14,6 +14,7 @@ from .chat_completion_message_tool_call import ChatCompletionMessageToolCall as
from .chat_completion_content_part_param import ChatCompletionContentPartParam as ChatCompletionContentPartParam
from .chat_completion_tool_message_param import ChatCompletionToolMessageParam as ChatCompletionToolMessageParam
from .chat_completion_user_message_param import ChatCompletionUserMessageParam as ChatCompletionUserMessageParam
+from .chat_completion_stream_options_param import ChatCompletionStreamOptionsParam as ChatCompletionStreamOptionsParam
from .chat_completion_system_message_param import ChatCompletionSystemMessageParam as ChatCompletionSystemMessageParam
from .chat_completion_function_message_param import (
ChatCompletionFunctionMessageParam as ChatCompletionFunctionMessageParam,
src/openai/types/chat/chat_completion_chunk.py
@@ -4,6 +4,7 @@ from typing import List, Optional
from typing_extensions import Literal
from ..._models import BaseModel
+from ..completion_usage import CompletionUsage
from .chat_completion_token_logprob import ChatCompletionTokenLogprob
__all__ = [
@@ -105,7 +106,8 @@ class ChatCompletionChunk(BaseModel):
choices: List[Choice]
"""A list of chat completion choices.
- Can be more than one if `n` is greater than 1.
+ Can contain more than one elements if `n` is greater than 1. Can also be empty
+ for the last chunk if you set `stream_options: {"include_usage": true}`.
"""
created: int
@@ -126,3 +128,11 @@ class ChatCompletionChunk(BaseModel):
Can be used in conjunction with the `seed` request parameter to understand when
backend changes have been made that might impact determinism.
"""
+
+ usage: Optional[CompletionUsage] = None
+ """
+ An optional field that will only be present when you set
+ `stream_options: {"include_usage": true}` in your request. When present, it
+ contains a null value except for the last chunk which contains the token usage
+ statistics for the entire request.
+ """
src/openai/types/chat/chat_completion_stream_options_param.py
@@ -0,0 +1,17 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from __future__ import annotations
+
+from typing_extensions import TypedDict
+
+__all__ = ["ChatCompletionStreamOptionsParam"]
+
+
+class ChatCompletionStreamOptionsParam(TypedDict, total=False):
+ include_usage: bool
+ """If set, an additional chunk will be streamed before the `data: [DONE]` message.
+
+ The `usage` field on this chunk shows the token usage statistics for the entire
+ request, and the `choices` field will always be an empty array. All other chunks
+ will also include a `usage` field, but with a null value.
+ """
src/openai/types/chat/completion_create_params.py
@@ -9,6 +9,7 @@ from ...types import shared_params
from ..chat_model import ChatModel
from .chat_completion_tool_param import ChatCompletionToolParam
from .chat_completion_message_param import ChatCompletionMessageParam
+from .chat_completion_stream_options_param import ChatCompletionStreamOptionsParam
from .chat_completion_tool_choice_option_param import ChatCompletionToolChoiceOptionParam
from .chat_completion_function_call_option_param import ChatCompletionFunctionCallOptionParam
@@ -141,6 +142,9 @@ class CompletionCreateParamsBase(TypedDict, total=False):
stop: Union[Optional[str], List[str]]
"""Up to 4 sequences where the API will stop generating further tokens."""
+ stream_options: Optional[ChatCompletionStreamOptionsParam]
+ """Options for streaming response. Only set this when you set `stream: true`."""
+
temperature: Optional[float]
"""What sampling temperature to use, between 0 and 2.
src/openai/types/completion_create_params.py
@@ -5,6 +5,8 @@ from __future__ import annotations
from typing import Dict, List, Union, Iterable, Optional
from typing_extensions import Literal, Required, TypedDict
+from .chat.chat_completion_stream_options_param import ChatCompletionStreamOptionsParam
+
__all__ = ["CompletionCreateParamsBase", "CompletionCreateParamsNonStreaming", "CompletionCreateParamsStreaming"]
@@ -123,6 +125,9 @@ class CompletionCreateParamsBase(TypedDict, total=False):
The returned text will not contain the stop sequence.
"""
+ stream_options: Optional[ChatCompletionStreamOptionsParam]
+ """Options for streaming response. Only set this when you set `stream: true`."""
+
suffix: Optional[str]
"""The suffix that comes after a completion of inserted text.
tests/api_resources/chat/test_completions.py
@@ -9,7 +9,9 @@ import pytest
from openai import OpenAI, AsyncOpenAI
from tests.utils import assert_matches_type
-from openai.types.chat import ChatCompletion
+from openai.types.chat import (
+ ChatCompletion,
+)
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
@@ -59,6 +61,7 @@ class TestCompletions:
seed=-9223372036854776000,
stop="string",
stream=False,
+ stream_options={"include_usage": True},
temperature=1,
tool_choice="none",
tools=[
@@ -172,6 +175,7 @@ class TestCompletions:
response_format={"type": "json_object"},
seed=-9223372036854776000,
stop="string",
+ stream_options={"include_usage": True},
temperature=1,
tool_choice="none",
tools=[
@@ -289,6 +293,7 @@ class TestAsyncCompletions:
seed=-9223372036854776000,
stop="string",
stream=False,
+ stream_options={"include_usage": True},
temperature=1,
tool_choice="none",
tools=[
@@ -402,6 +407,7 @@ class TestAsyncCompletions:
response_format={"type": "json_object"},
seed=-9223372036854776000,
stop="string",
+ stream_options={"include_usage": True},
temperature=1,
tool_choice="none",
tools=[
tests/api_resources/test_completions.py
@@ -41,6 +41,7 @@ class TestCompletions:
seed=-9223372036854776000,
stop="\n",
stream=False,
+ stream_options={"include_usage": True},
suffix="test.",
temperature=1,
top_p=1,
@@ -99,6 +100,7 @@ class TestCompletions:
presence_penalty=-2,
seed=-9223372036854776000,
stop="\n",
+ stream_options={"include_usage": True},
suffix="test.",
temperature=1,
top_p=1,
@@ -161,6 +163,7 @@ class TestAsyncCompletions:
seed=-9223372036854776000,
stop="\n",
stream=False,
+ stream_options={"include_usage": True},
suffix="test.",
temperature=1,
top_p=1,
@@ -219,6 +222,7 @@ class TestAsyncCompletions:
presence_penalty=-2,
seed=-9223372036854776000,
stop="\n",
+ stream_options={"include_usage": True},
suffix="test.",
temperature=1,
top_p=1,
.stats.yml
@@ -1,2 +1,2 @@
configured_endpoints: 64
-openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-97c9a5f089049dc9eb5cee9475558049003e37e42202cab39e59d75e08b4c613.yml
+openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-edb5af3ade0cd27cf366b0654b90c7a81c43c433e11fc3f6e621e2c779de10d4.yml
api.md
@@ -43,6 +43,7 @@ from openai.types.chat import (
ChatCompletionMessageToolCall,
ChatCompletionNamedToolChoice,
ChatCompletionRole,
+ ChatCompletionStreamOptions,
ChatCompletionSystemMessageParam,
ChatCompletionTokenLogprob,
ChatCompletionTool,