diff --git a/libs/core/langchain_core/messages/content_blocks.py b/libs/core/langchain_core/messages/content_blocks.py index 27f21f5b3a4..6aa9e2ee5ba 100644 --- a/libs/core/langchain_core/messages/content_blocks.py +++ b/libs/core/langchain_core/messages/content_blocks.py @@ -4,50 +4,50 @@ import warnings from typing import Any, Literal, Union from pydantic import TypeAdapter, ValidationError -from typing_extensions import NotRequired, TypedDict +from typing_extensions import Required, TypedDict # Text and annotations class UrlCitation(TypedDict, total=False): """Citation from a URL.""" - type: Literal["url_citation"] + type: Required[Literal["url_citation"]] - url: str + url: Required[str] """Source URL.""" - title: NotRequired[str] + title: str """Source title.""" - cited_text: NotRequired[str] + cited_text: str """Text from the source that is being cited.""" - start_index: NotRequired[int] + start_index: int """Start index of the response text for which the annotation applies.""" - end_index: NotRequired[int] + end_index: int """End index of the response text for which the annotation applies.""" class DocumentCitation(TypedDict, total=False): """Annotation for data from a document.""" - type: Literal["document_citation"] + type: Required[Literal["document_citation"]] - title: NotRequired[str] + title: str """Source title.""" - cited_text: NotRequired[str] + cited_text: str """Text from the source that is being cited.""" - start_index: NotRequired[int] + start_index: int """Start index of the response text for which the annotation applies.""" - end_index: NotRequired[int] + end_index: int """End index of the response text for which the annotation applies.""" -class NonStandardAnnotation(TypedDict, total=False): +class NonStandardAnnotation(TypedDict): """Provider-specific annotation format.""" type: Literal["non_standard_annotation"] @@ -59,13 +59,11 @@ class NonStandardAnnotation(TypedDict, total=False): class TextContentBlock(TypedDict, total=False): """Content block for text output.""" - type: Literal["text"] + type: Required[Literal["text"]] """Type of the content block.""" - text: str + text: Required[str] """Block text.""" - annotations: NotRequired[ - list[Union[UrlCitation, DocumentCitation, NonStandardAnnotation]] - ] + annotations: list[Union[UrlCitation, DocumentCitation, NonStandardAnnotation]] """Citations and other annotations.""" @@ -77,9 +75,9 @@ class ToolCallContentBlock(TypedDict, total=False): message's ``tool_calls`` attribute. """ - type: Literal["tool_call"] + type: Required[Literal["tool_call"]] """Type of the content block.""" - id: str + id: Required[str] """Tool call ID.""" @@ -87,9 +85,9 @@ class ToolCallContentBlock(TypedDict, total=False): class ReasoningContentBlock(TypedDict, total=False): """Content block for reasoning output.""" - type: Literal["reasoning"] + type: Required[Literal["reasoning"]] """Type of the content block.""" - reasoning: NotRequired[str] + reasoning: str """Reasoning text.""" @@ -97,51 +95,51 @@ class ReasoningContentBlock(TypedDict, total=False): class BaseDataContentBlock(TypedDict, total=False): """Base class for data content blocks.""" - mime_type: NotRequired[str] + mime_type: str """MIME type of the content block (if needed).""" class URLContentBlock(BaseDataContentBlock): """Content block for data from a URL.""" - type: Literal["image", "audio", "file"] + type: Required[Literal["image", "audio", "file"]] """Type of the content block.""" - source_type: Literal["url"] + source_type: Required[Literal["url"]] """Source type (url).""" - url: str + url: Required[str] """URL for data.""" class Base64ContentBlock(BaseDataContentBlock): """Content block for inline data from a base64 string.""" - type: Literal["image", "audio", "file"] + type: Required[Literal["image", "audio", "file"]] """Type of the content block.""" - source_type: Literal["base64"] + source_type: Required[Literal["base64"]] """Source type (base64).""" - data: str + data: Required[str] """Data as a base64 string.""" class PlainTextContentBlock(BaseDataContentBlock): """Content block for plain text data (e.g., from a document).""" - type: Literal["file"] + type: Required[Literal["file"]] """Type of the content block.""" - source_type: Literal["text"] + source_type: Required[Literal["text"]] """Source type (text).""" - text: str + text: Required[str] """Text data.""" -class IDContentBlock(TypedDict): +class IDContentBlock(BaseDataContentBlock): """Content block for data specified by an identifier.""" - type: Literal["image", "audio", "file"] + type: Required[Literal["image", "audio", "file"]] """Type of the content block.""" - source_type: Literal["id"] + source_type: Required[Literal["id"]] """Source type (id).""" - id: str + id: Required[str] """Identifier for data source.""" @@ -156,7 +154,7 @@ _DataContentBlockAdapter: TypeAdapter[DataContentBlock] = TypeAdapter(DataConten # Non-standard -class NonStandardContentBlock(TypedDict, total=False): +class NonStandardContentBlock(TypedDict): """Content block provider-specific data. This block contains data for which there is not yet a standard type.