Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions providers/anthropic/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ func toPrompt(prompt fantasy.Prompt, sendReasoningData bool) ([]anthropic.TextBl
}
}
}
if !hasVisibleUserContent(anthropicContent) {
warnings = append(warnings, fantasy.CallWarning{
Type: fantasy.CallWarningTypeOther,
Message: "dropping empty user message (contains neither user-facing content nor tool results)",
})
continue
}
messages = append(messages, anthropic.NewUserMessage(anthropicContent...))
case fantasy.MessageRoleAssistant:
var anthropicContent []anthropic.ContentBlockParamUnion
Expand Down Expand Up @@ -651,15 +658,15 @@ func toPrompt(prompt fantasy.Prompt, sendReasoningData bool) ([]anthropic.TextBl
}
if !sendReasoningData {
warnings = append(warnings, fantasy.CallWarning{
Type: "other",
Type: fantasy.CallWarningTypeOther,
Message: "sending reasoning content is disabled for this model",
})
continue
}
reasoningMetadata := GetReasoningMetadata(part.Options())
if reasoningMetadata == nil {
warnings = append(warnings, fantasy.CallWarning{
Type: "other",
Type: fantasy.CallWarningTypeOther,
Message: "unsupported reasoning metadata",
})
continue
Expand All @@ -671,7 +678,7 @@ func toPrompt(prompt fantasy.Prompt, sendReasoningData bool) ([]anthropic.TextBl
anthropicContent = append(anthropicContent, anthropic.NewRedactedThinkingBlock(reasoningMetadata.RedactedData))
} else {
warnings = append(warnings, fantasy.CallWarning{
Type: "other",
Type: fantasy.CallWarningTypeOther,
Message: "unsupported reasoning metadata",
})
continue
Expand Down Expand Up @@ -701,12 +708,38 @@ func toPrompt(prompt fantasy.Prompt, sendReasoningData bool) ([]anthropic.TextBl
}
}
}

if !hasVisibleAssistantContent(anthropicContent) {
warnings = append(warnings, fantasy.CallWarning{
Type: fantasy.CallWarningTypeOther,
Message: "dropping empty assistant message (contains neither user-facing content nor tool calls)",
})
continue
}
messages = append(messages, anthropic.NewAssistantMessage(anthropicContent...))
}
}
return systemBlocks, messages, warnings
}

func hasVisibleUserContent(content []anthropic.ContentBlockParamUnion) bool {
for _, block := range content {
if block.OfText != nil || block.OfImage != nil || block.OfToolResult != nil {
return true
}
}
return false
}

func hasVisibleAssistantContent(content []anthropic.ContentBlockParamUnion) bool {
for _, block := range content {
if block.OfText != nil || block.OfToolUse != nil {
return true
}
}
return false
}

func mapFinishReason(finishReason string) fantasy.FinishReason {
switch finishReason {
case "end_turn", "pause_turn", "stop_sequence":
Expand Down
Loading
Loading