Skip to content

ResponseOutputTextAnnotationAddedEvent.annotation typed as object instead of Annotation #3419

Description

@Tesla2000

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions