diff --git a/src/openai_schemas.py b/src/openai_schemas.py index 5b54ddd9..9f4a0928 100644 --- a/src/openai_schemas.py +++ b/src/openai_schemas.py @@ -72,13 +72,36 @@ class UsageStats: cache_creation_tokens: int = 0 -@dataclass(frozen=True) +@dataclass(frozen=True, init=False) class NormalizedResponse: text: str tool_calls: tuple[ToolCall, ...] usage: UsageStats raw_response: Any + def __init__( + self, + text: str, + tool_calls: tuple[ToolCall, ...] = (), + usage: UsageStats | None = None, + raw_response: Any = None, + usage_input_tokens: int | None = None, + usage_output_tokens: int | None = None, + usage_cache_read_tokens: int | None = None, + usage_cache_creation_tokens: int | None = None, + ) -> None: + if usage is None: + usage = UsageStats( + input_tokens=usage_input_tokens if usage_input_tokens is not None else 0, + output_tokens=usage_output_tokens if usage_output_tokens is not None else 0, + cache_read_tokens=usage_cache_read_tokens if usage_cache_read_tokens is not None else 0, + cache_creation_tokens=usage_cache_creation_tokens if usage_cache_creation_tokens is not None else 0, + ) + object.__setattr__(self, "text", text) + object.__setattr__(self, "tool_calls", tool_calls) + object.__setattr__(self, "usage", usage) + object.__setattr__(self, "raw_response", raw_response) + def to_legacy_dict(self) -> JsonValue: return { "text": self.text,