Skip to content

Commit b7ad7c2

Browse files
committed
allow tools args to be an empty string
1 parent abdd7e6 commit b7ad7c2

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Sources/Converse/ContentBlocks/JSON.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ public struct JSON: Codable, @unchecked Sendable { // FIXME: make Sendable
6060
}
6161

6262
public init(from string: String) throws {
63-
guard let data = string.data(using: .utf8) else {
63+
var s: String!
64+
if string.isEmpty {
65+
s = "{}"
66+
} else {
67+
s = string
68+
}
69+
guard let data = s.data(using: .utf8) else {
6470
throw BedrockLibraryError.encodingError("Could not encode String to Data")
6571
}
6672
try self.init(from: data)

Sources/Converse/Streaming/ConverseReplyStream.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,9 @@ public struct ConverseReplyStream: Sendable {
267267
// contentBlock[currentBlockId] = .reasoning(bufferReasoningData)
268268
// bufferReasoningData = Data()
269269
// }
270-
if !state.bufferToolUse.isEmpty {
271-
guard let toolUseStart = state.toolUseStart else {
272-
throw BedrockLibraryError.invalidSDKType("Received a tool use delta without tool use start")
273-
}
270+
271+
// when there is a toolUse, the bufferToolUse may be empty, when the model found out that there is not arguments to pass. Therefore, I just check the presence of the toolUseStart
272+
if let toolUseStart = state.toolUseStart {
274273
let json = try JSON(from: state.bufferToolUse)
275274
state.lastContentBlock = (
276275
state.currentBlockId, .toolUse(.init(id: toolUseStart.id, name: toolUseStart.name, input: json))

Tests/Converse/JSONTests.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import Testing
1818
@testable import BedrockService
1919

2020
// MARK: JSON
21-
extension BedrockServiceTests {
21+
@Suite("JSONTests")
22+
struct JSONTests {
2223

2324
@Test("JSON getValue")
2425
func jsonGetValue() async throws {
@@ -126,4 +127,12 @@ extension BedrockServiceTests {
126127
let _ = try JSON(from: invalidJSONString)
127128
}
128129
}
130+
131+
@Test("Empty JSON")
132+
func emptyJSON() async throws {
133+
#expect(throws: Never.self) {
134+
let json = try JSON(from: "")
135+
#expect(json.getValue("nonExistentKey") == nil)
136+
}
137+
}
129138
}

0 commit comments

Comments
 (0)