Skip to content

Commit 863ff62

Browse files
authored
fix: HOTFIX anthropic: strip thinking without signature and empty text parts (#8329)
fix: strip thinking without signature and empty text parts
1 parent ddf4e0f commit 863ff62

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

core/llm/llms/Anthropic.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,36 @@ class Anthropic extends BaseLLM {
8787
private convertMessageContentToBlocks(
8888
content: MessageContent,
8989
): ContentBlockParam[] {
90+
const parts: ContentBlockParam[] = [];
9091
if (typeof content === "string") {
91-
return [
92-
{
92+
if (content) {
93+
parts.push({
9394
type: "text",
9495
text: content,
95-
},
96-
];
97-
}
98-
return content.map((part) => {
99-
if (part.type === "text") {
100-
return {
101-
type: "text",
102-
text: part.text,
103-
};
96+
});
10497
}
105-
return {
106-
type: "image",
107-
source: {
108-
type: "base64",
109-
media_type: getAnthropicMediaTypeFromDataUrl(part.imageUrl.url),
110-
data: part.imageUrl.url.split(",")[1],
111-
},
112-
};
113-
});
98+
} else {
99+
for (const part of content) {
100+
if (part.type === "text") {
101+
if (part.text) {
102+
parts.push({
103+
type: "text",
104+
text: part.text,
105+
});
106+
}
107+
} else {
108+
parts.push({
109+
type: "image",
110+
source: {
111+
type: "base64",
112+
media_type: getAnthropicMediaTypeFromDataUrl(part.imageUrl.url),
113+
data: part.imageUrl.url.split(",")[1],
114+
},
115+
});
116+
}
117+
}
118+
}
119+
return parts;
114120
}
115121

116122
private convertToolCallsToBlocks(
@@ -152,20 +158,30 @@ class Anthropic extends BaseLLM {
152158
},
153159
];
154160
}
161+
// Strip thinking that has no signature
162+
const signature = message.signature;
163+
if (!signature) {
164+
return [];
165+
}
155166
if (typeof message.content === "string") {
167+
if (!message.content) {
168+
return [];
169+
}
156170
return [
157171
{
158172
type: "thinking",
159173
thinking: message.content,
160-
signature: message.signature ?? "", // TODO - unsafe signature
174+
signature,
161175
},
162176
];
163177
}
164-
const textParts = message.content.filter((p) => p.type === "text");
178+
const textParts = message.content
179+
.filter((p) => p.type === "text")
180+
.filter((p) => !!p.text);
165181
return textParts.map((part) => ({
166182
type: "thinking",
167183
thinking: part.text,
168-
signature: message.signature ?? "", // TODO - unsafe signature
184+
signature,
169185
}));
170186
case "assistant":
171187
const blocks: ContentBlockParam[] = this.convertMessageContentToBlocks(

0 commit comments

Comments
 (0)