Skip to content

Commit 45fdae4

Browse files
ci: Add openai to uv typing group (#7334)
Add `openai` to the typing dependency group and fix the resulting mypy errors.
1 parent 747eaa8 commit 45fdae4

6 files changed

Lines changed: 123 additions & 56 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ typing = [
7676
"huggingface-hub>=1.26.1",
7777
"anthropic>=1.2.0",
7878
"google-genai>=2.21.0",
79+
"openai>=3.7.0",
7980
]
8081
test = [
8182
"dataclasses ; python_full_version < '3.7'",

sentry_sdk/ai/_openai_completions_api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from collections.abc import Iterable
2-
from typing import TYPE_CHECKING
2+
from typing import TYPE_CHECKING, cast
33

44
if TYPE_CHECKING:
5-
from typing import Union
5+
from typing import TypeGuard, Union
66

77
from openai.types.chat import (
88
ChatCompletionContentPartParam,
9+
ChatCompletionContentPartRefusalParam,
10+
ChatCompletionContentPartTextParam,
911
ChatCompletionMessageParam,
1012
ChatCompletionSystemMessageParam,
1113
ChatCompletionToolUnionParam,
@@ -14,32 +16,34 @@
1416
from sentry_sdk._types import TextPart, ToolDefinition
1517

1618

17-
def _is_system_instruction(message: "ChatCompletionMessageParam") -> bool:
19+
def _is_system_instruction(
20+
message: "ChatCompletionMessageParam",
21+
) -> "TypeGuard[ChatCompletionSystemMessageParam]":
1822
return isinstance(message, dict) and message.get("role") == "system"
1923

2024

2125
def _get_system_instructions(
2226
messages: "Iterable[ChatCompletionMessageParam]",
23-
) -> "list[ChatCompletionMessageParam]":
27+
) -> "list[ChatCompletionSystemMessageParam]":
2428
if not isinstance(messages, Iterable):
2529
return []
2630

2731
return [message for message in messages if _is_system_instruction(message)]
2832

2933

3034
def _get_text_items(
31-
content: "Union[str, Iterable[ChatCompletionContentPartParam]]",
35+
content: "Union[str, Iterable[Union[ChatCompletionContentPartParam, ChatCompletionContentPartTextParam, ChatCompletionContentPartRefusalParam]]]",
3236
) -> "list[str]":
3337
if isinstance(content, str):
3438
return [content]
3539

3640
if not isinstance(content, Iterable):
3741
return []
3842

39-
text_items = []
43+
text_items: "list[str]" = []
4044
for part in content:
4145
if isinstance(part, dict) and part.get("type") == "text":
42-
text = part.get("text", None)
46+
text = cast("ChatCompletionContentPartTextParam", part).get("text", None)
4347
if text is not None:
4448
text_items.append(text)
4549

sentry_sdk/ai/_openai_responses_api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import TYPE_CHECKING, Iterable, cast
22

33
if TYPE_CHECKING:
4-
from typing import Iterable, Union
4+
from typing import Iterable, TypeGuard, Union
55

66
from openai.types.responses import (
77
ResponseInputItemParam,
@@ -15,7 +15,9 @@
1515
from sentry_sdk._types import TextPart, ToolDefinition
1616

1717

18-
def _is_system_instruction(message: "ResponseInputItemParam") -> bool:
18+
def _is_system_instruction(
19+
message: "ResponseInputItemParam",
20+
) -> "TypeGuard[Union[EasyInputMessageParam, Message]]":
1921
if not isinstance(message, dict) or not message.get("role") == "system":
2022
return False
2123

@@ -24,7 +26,7 @@ def _is_system_instruction(message: "ResponseInputItemParam") -> bool:
2426

2527
def _get_system_instructions(
2628
messages: "Union[str, ResponseInputParam]",
27-
) -> "list[ResponseInputItemParam]":
29+
) -> "list[Union[EasyInputMessageParam, Message]]":
2830
if not isinstance(messages, list):
2931
return []
3032

0 commit comments

Comments
 (0)