Python: added generic types to ChatOptions and ChatResponse/AgentResponse for Response Format (#3305)

* added generic types to ChatOptions and ChatResponse/AgentResponse for response format

* fix typevar import

* fix for older python versions

* fix missing import

* fixed imports

* fixed mypy

* mypy fix
This commit is contained in:
Eduard van Valkenburg
2026-01-28 21:23:02 +00:00
committed by GitHub
parent 1f8463f9bb
commit 1226828ec2
42 changed files with 486 additions and 281 deletions
@@ -2,7 +2,7 @@
import sys
from collections.abc import AsyncIterable, MutableMapping, MutableSequence, Sequence
from typing import Any, ClassVar, Final, Generic, Literal, TypedDict
from typing import Any, ClassVar, Final, Generic, Literal
from agent_framework import (
AGENT_FRAMEWORK_USER_AGENT,
@@ -47,15 +47,18 @@ from anthropic.types.beta.beta_code_execution_tool_result_error import (
)
from pydantic import BaseModel, SecretStr, ValidationError
if sys.version_info >= (3, 13):
from typing import TypeVar
if sys.version_info >= (3, 11):
from typing import TypedDict # type: ignore # pragma: no cover
else:
from typing_extensions import TypeVar
from typing_extensions import TypedDict # type: ignore # pragma: no cover
if sys.version_info >= (3, 13):
from typing import TypeVar # type: ignore # pragma: no cover
else:
from typing_extensions import TypeVar # type: ignore # pragma: no cover
if sys.version_info >= (3, 12):
from typing import override # type: ignore # pragma: no cover
else:
from typing_extensions import override # type: ignore[import] # pragma: no cover
from typing_extensions import override # type: ignore # pragma: no cover
__all__ = [
"AnthropicChatOptions",
@@ -69,6 +72,8 @@ ANTHROPIC_DEFAULT_MAX_TOKENS: Final[int] = 1024
BETA_FLAGS: Final[list[str]] = ["mcp-client-2025-04-04", "code-execution-2025-08-25"]
STRUCTURED_OUTPUTS_BETA_FLAG: Final[str] = "structured-outputs-2025-11-13"
TResponseModel = TypeVar("TResponseModel", bound=BaseModel | None, default=None)
# region Anthropic Chat Options TypedDict
@@ -91,7 +96,7 @@ class ThinkingConfig(TypedDict, total=False):
budget_tokens: int
class AnthropicChatOptions(ChatOptions, total=False):
class AnthropicChatOptions(ChatOptions[TResponseModel], Generic[TResponseModel], total=False):
"""Anthropic-specific chat options.
Extends ChatOptions with options specific to Anthropic's Messages API.