Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): correct types for attachments #1342

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@

print(chunk.choices[0].delta.content, end="")
print()

# Response headers:
print("----- custom response headers test -----")
response = client.chat.completions.with_raw_response.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
)
completion = response.parse()
print(response.request_id)
print(completion.choices[0].message.content)
6 changes: 4 additions & 2 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class EventHandler(AssistantEventHandler):
def on_text_delta(self, delta: TextDelta, snapshot: Text):
print(delta.value, end="", flush=True)

@override
def on_tool_call_created(self, tool_call: ToolCall):
print(f"\nassistant > {tool_call.type}\n", flush=True)

@override
def on_tool_call_delta(self, delta: ToolCallDelta, snapshot: ToolCall):
if delta.type == 'code_interpreter':
if delta.type == "code_interpreter" and delta.code_interpreter:
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
Expand Down Expand Up @@ -69,7 +71,7 @@ with client.beta.threads.runs.stream(
) as stream:
for event in stream:
# Print the text from text delta events
if event.type == "thread.message.delta" and event.data.delta.content:
if event.event == "thread.message.delta" and event.data.delta.content:
print(event.data.delta.content[0].text)
```

Expand Down
4 changes: 4 additions & 0 deletions src/openai/_legacy_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def __init__(
self._options = options
self.http_response = raw

@property
def request_id(self) -> str | None:
return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]

@overload
def parse(self, *, to: type[_T]) -> _T:
...
Expand Down
8 changes: 8 additions & 0 deletions src/openai/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:


class APIResponse(BaseAPIResponse[R]):
@property
def request_id(self) -> str | None:
return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]

@overload
def parse(self, *, to: type[_T]) -> _T:
...
Expand Down Expand Up @@ -362,6 +366,10 @@ def iter_lines(self) -> Iterator[str]:


class AsyncAPIResponse(BaseAPIResponse[R]):
@property
def request_id(self) -> str | None:
return self.http_response.headers.get("x-request-id") # type: ignore[no-any-return]

@overload
async def parse(self, *, to: type[_T]) -> _T:
...
Expand Down
4 changes: 2 additions & 2 deletions src/openai/types/beta/thread_create_and_run_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ class ThreadCreateAndRunParamsBase(TypedDict, total=False):


class ThreadMessageAttachment(TypedDict, total=False):
add_to: List[Literal["file_search", "code_interpreter"]]

file_id: str
"""The ID of the file to attach to the message."""

tools: List[Literal["file_search", "code_interpreter"]]


class ThreadMessage(TypedDict, total=False):
content: Required[str]
Expand Down
4 changes: 2 additions & 2 deletions src/openai/types/beta/thread_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class ThreadCreateParams(TypedDict, total=False):


class MessageAttachment(TypedDict, total=False):
add_to: List[Literal["file_search", "code_interpreter"]]

file_id: str
"""The ID of the file to attach to the message."""

tools: List[Literal["file_search", "code_interpreter"]]


class Message(TypedDict, total=False):
content: Required[str]
Expand Down
4 changes: 2 additions & 2 deletions src/openai/types/beta/threads/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@


class Attachment(BaseModel):
add_to: Optional[List[Literal["file_search", "code_interpreter"]]] = None

file_id: Optional[str] = None
"""The ID of the file to attach to the message."""

tools: Optional[List[Literal["file_search", "code_interpreter"]]] = None


class IncompleteDetails(BaseModel):
reason: Literal["content_filter", "max_tokens", "run_cancelled", "run_expired", "run_failed"]
Expand Down
4 changes: 2 additions & 2 deletions src/openai/types/beta/threads/message_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MessageCreateParams(TypedDict, total=False):


class Attachment(TypedDict, total=False):
add_to: List[Literal["file_search", "code_interpreter"]]

file_id: str
"""The ID of the file to attach to the message."""

tools: List[Literal["file_search", "code_interpreter"]]
4 changes: 2 additions & 2 deletions src/openai/types/beta/threads/run_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ class RunCreateParamsBase(TypedDict, total=False):


class AdditionalMessageAttachment(TypedDict, total=False):
add_to: List[Literal["file_search", "code_interpreter"]]

file_id: str
"""The ID of the file to attach to the message."""

tools: List[Literal["file_search", "code_interpreter"]]


class AdditionalMessage(TypedDict, total=False):
content: Required[str]
Expand Down
Loading