[Bugfix][Frontend] Accept Anthropic tool_addition and tool_removal content blocks (#57693)
Signed-off-by: Anurag Singh <dr.anuragkr@gmail.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Antigravity <antigravity@google.com> Co-authored-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
co-authored by
Antigravity
sfeng33
parent
e6674262f1
commit
28c57456db
@@ -44,6 +44,8 @@ class AnthropicContentBlock(BaseModel):
|
||||
"tool_reference",
|
||||
"thinking",
|
||||
"redacted_thinking",
|
||||
"tool_addition",
|
||||
"tool_removal",
|
||||
]
|
||||
text: str | None = None
|
||||
# For image content
|
||||
@@ -62,6 +64,28 @@ class AnthropicContentBlock(BaseModel):
|
||||
signature: str | None = None
|
||||
# For redacted thinking content (safety-filtered by the API)
|
||||
data: str | None = None
|
||||
# For tool_addition/tool_removal content
|
||||
tool: "AnthropicToolChange | None" = None
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_tool_change(self) -> "AnthropicContentBlock":
|
||||
if self.type not in ("tool_addition", "tool_removal"):
|
||||
return self
|
||||
if self.tool is None:
|
||||
raise ValueError(f"tool is required for {self.type} blocks")
|
||||
if self.type == "tool_removal" and self.tool.type != "tool_reference":
|
||||
raise ValueError("tool_removal only accepts a tool_reference")
|
||||
return self
|
||||
|
||||
|
||||
def _reject_tool_changes(content: str | list[AnthropicContentBlock] | None) -> None:
|
||||
if content is None or isinstance(content, str):
|
||||
return
|
||||
for block in content:
|
||||
if block.type in ("tool_addition", "tool_removal"):
|
||||
raise ValueError(
|
||||
f'{block.type} blocks are only allowed in messages with role "system"'
|
||||
)
|
||||
|
||||
|
||||
class AnthropicMessage(BaseModel):
|
||||
@@ -70,6 +94,12 @@ class AnthropicMessage(BaseModel):
|
||||
role: Literal["user", "assistant", "system"]
|
||||
content: str | list[AnthropicContentBlock]
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_tool_change_role(self) -> "AnthropicMessage":
|
||||
if self.role != "system":
|
||||
_reject_tool_changes(self.content)
|
||||
return self
|
||||
|
||||
|
||||
class AnthropicTool(BaseModel):
|
||||
"""Tool definition."""
|
||||
@@ -90,6 +120,26 @@ class AnthropicTool(BaseModel):
|
||||
return v
|
||||
|
||||
|
||||
class AnthropicToolChangeReference(BaseModel):
|
||||
"""A tool named by a tool_addition or tool_removal block."""
|
||||
|
||||
type: Literal["tool_reference"]
|
||||
name: str
|
||||
|
||||
|
||||
class AnthropicToolChangeDefinition(BaseModel):
|
||||
"""A tool defined by value in a tool_addition block."""
|
||||
|
||||
type: Literal["tool_definition"]
|
||||
definition: AnthropicTool
|
||||
|
||||
|
||||
AnthropicToolChange = Annotated[
|
||||
AnthropicToolChangeReference | AnthropicToolChangeDefinition,
|
||||
Field(discriminator="type"),
|
||||
]
|
||||
|
||||
|
||||
class AnthropicToolChoice(BaseModel):
|
||||
"""Tool Choice definition."""
|
||||
|
||||
@@ -231,6 +281,12 @@ class AnthropicMessagesRequest(BaseModel):
|
||||
raise ValueError("max_tokens must be positive")
|
||||
return v
|
||||
|
||||
@field_validator("system")
|
||||
@classmethod
|
||||
def validate_system(cls, v):
|
||||
_reject_tool_changes(v)
|
||||
return v
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_thinking_budget(self) -> "AnthropicMessagesRequest":
|
||||
# P/D prefill legs are sent with max_tokens=1 and never decode; the
|
||||
@@ -343,6 +399,12 @@ class AnthropicCountTokensRequest(BaseModel):
|
||||
raise ValueError("Model is required")
|
||||
return v
|
||||
|
||||
@field_validator("system")
|
||||
@classmethod
|
||||
def validate_system(cls, v):
|
||||
_reject_tool_changes(v)
|
||||
return v
|
||||
|
||||
|
||||
class AnthropicCountTokensResponse(BaseModel):
|
||||
"""Anthropic messages.count_tokens response."""
|
||||
|
||||
Reference in New Issue
Block a user