Skip to content
Merged
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
9 changes: 8 additions & 1 deletion Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -999,10 +999,17 @@ extension Transcript {
)
)
case .response(let response):
// Anthropic rejects text blocks without non-whitespace text,
// such as the empty response of a turn that only called tools.
let content = convertSegmentsToAnthropicContent(response.segments).filter { block in
guard case .text(let text) = block else { return true }
return !text.text.allSatisfy(\.isWhitespace)
}
guard !content.isEmpty else { continue }
messages.append(
.init(
role: .assistant,
content: convertSegmentsToAnthropicContent(response.segments)
content: content
)
)
case .toolCalls(let toolCalls):
Expand Down
33 changes: 31 additions & 2 deletions Tests/AnyLanguageModelTests/AnthropicLanguageModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ struct AnthropicLanguageModelTests {

@Suite("Anthropic request encoding", .serialized)
struct AnthropicRequestTests {
private func makeSession() -> LanguageModelSession {
private func makeSession(transcript: Transcript = Transcript()) -> LanguageModelSession {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [AnthropicRequestURLProtocol.self]
let model = AnthropicLanguageModel(
apiKey: "test-key",
model: "test-model",
session: URLSession(configuration: configuration)
)
return LanguageModelSession(model: model)
return LanguageModelSession(model: model, transcript: transcript)
}

private func requestBody() throws -> [String: JSONValue] {
Expand Down Expand Up @@ -262,6 +262,35 @@ struct AnthropicLanguageModelTests {
#expect(body["output_config"] == nil)
}

@Test(arguments: ["", " \n"])
func blankResponseIsOmittedFromHistory(responseText: String) async throws {
let arguments = try GeneratedContent(json: #"{"city":"Cupertino"}"#)
let transcript = Transcript(entries: [
.prompt(Transcript.Prompt(segments: [.text(.init(content: "Weather?"))])),
.toolCalls(
Transcript.ToolCalls([
Transcript.ToolCall(id: "call-1", toolName: "getWeather", arguments: arguments)
])
),
.toolOutput(
Transcript.ToolOutput(
id: "call-1",
toolName: "getWeather",
segments: [.text(.init(content: "Sunny"))]
)
),
.response(Transcript.Response(assetIDs: [], segments: [.text(.init(content: responseText))])),
])
_ = try await makeSession(transcript: transcript).respond(to: "Thanks")

let messages = try #require(requestBody()["messages"]?.arrayValue)
let roles = messages.map { $0.objectValue?["role"]?.stringValue }
#expect(roles == ["user", "assistant", "user", "user"])
let texts = messages.flatMap { $0.objectValue?["content"]?.arrayValue ?? [] }
.compactMap { $0.objectValue?["text"]?.stringValue }
#expect(texts == ["Weather?", "Thanks"])
}

@Test func extraBodyOverridesCustomOptions() async throws {
var options = GenerationOptions()
options[custom: AnthropicLanguageModel.self] = .init(
Expand Down
Loading