Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
The annotation field on ResponseOutputTextAnnotationAddedEvent is typed as object:
class ResponseOutputTextAnnotationAddedEvent(BaseModel):
annotation: object
"""The annotation object being added. (See annotation schema for details.)"""
However, response_output_text.py already defines the correct union type:
Annotation: TypeAlias = Annotated[
Union[AnnotationFileCitation, AnnotationURLCitation, AnnotationContainerFileCitation, AnnotationFilePath],
PropertyInfo(discriminator="type"),
]
This forces consumers to either suppress type errors or use typing.cast to access typed fields:
# workaround required because annotation is typed as object
annotation = typing.cast(AnnotationURLCitation, stream_event.annotation)
title = annotation.title
url = annotation.url
The fix is to use the existing Annotation type alias:
class ResponseOutputTextAnnotationAddedEvent(BaseModel):
annotation: Annotation
This is consistent with how ResponseOutputText.annotations: List[Annotation] is already typed in the same file, and would allow consumers to match on annotation.type and access fields without casts.
To Reproduce
from openai.types.responses import ResponseOutputTextAnnotationAddedEvent
from openai.types.responses.response_output_text import AnnotationURLCitation
event = ResponseOutputTextAnnotationAddedEvent(
annotation={"type": "url_citation", "title": "Example", "url": "https://example.com", "start_index": 0, "end_index": 10},
annotation_index=0,
content_index=0,
item_id="item_123",
output_index=0,
sequence_number=1,
type="response.output_text.annotation.added",
)
# annotation is typed as `object` — none of these work without a cast
reveal_type(event.annotation) # Revealed type: object
print(event.annotation.url) # error: Cannot access attribute "url" for class "object"
Running basedpyright on the above produces:
error: Cannot access attribute "url" for class "object"
Attribute "url" is unknown (reportAttributeAccessIssue)
The consumer is forced to cast:
annotation = typing.cast(AnnotationURLCitation, event.annotation)
print(annotation.url) # works, but defeats the purpose of static typing
Code snippets
OS
Debian GNU/Linux 12 (bookworm)
Python version
Python v3.14.6
Library version
openai==2.38.0
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
The
annotationfield onResponseOutputTextAnnotationAddedEventis typed asobject:However,
response_output_text.pyalready defines the correct union type:This forces consumers to either suppress type errors or use
typing.castto access typed fields:The fix is to use the existing
Annotationtype alias:This is consistent with how
ResponseOutputText.annotations: List[Annotation]is already typed in the same file, and would allow consumers to match onannotation.typeand access fields without casts.To Reproduce
Running basedpyright on the above produces:
The consumer is forced to cast:
Code snippets
OS
Debian GNU/Linux 12 (bookworm)
Python version
Python v3.14.6
Library version
openai==2.38.0