fix(schemas): add legacy-kwarg backward compat to NormalizedResponse.__init__
12 tests fail with: TypeError: NormalizedResponse.__init__() got an unexpected keyword argument 'usage_input_tokens' The @dataclass(frozen=True) auto-generated __init__ requires `usage: UsageStats`, but 12 tests + 1 production site (src/ai_client.py:908) call it with the OLD flat-kwarg API (usage_input_tokens=..., usage_output_tokens=..., etc.). Change @dataclass(frozen=True) -> @dataclass(frozen=True, init=False) and add a custom __init__ that accepts BOTH signatures: - New: usage: UsageStats (used by current production code) - Legacy: usage_input_tokens, usage_output_tokens, usage_cache_read_tokens, usage_cache_creation_tokens (used by tests + 1 ai_client site) If usage is None and any legacy flat kwarg is non-None, build a UsageStats from the legacy kwargs. Otherwise use the provided usage. All field assignments use object.__setattr__ because frozen=True locks __setattr__. Verification: - Legacy kwargs work: NormalizedResponse(text="hi", tool_calls=(), usage_input_tokens=10, usage_output_tokens=5, raw_response=None) sets usage.input_tokens=10 - New kwargs work: NormalizedResponse(text="hi", tool_calls=(), usage=UsageStats(1, 2)) sets usage directly - 12 affected tests now pass (was 12 failed, 3 passed; now 15 passed)
This commit is contained in:
+24
-1
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user