Skip to content

Better handling of whitespace-only content #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ellmer (development version)

* `chat_claude()` and `chat_bedrock()` no longer choke after receiving an
output that consists only of whitespace (#376).

* `live_browser()` now initializes `shinychat::chat_ui()` with the messages from
the chat turns, rather than replaying the turns server-side (#381).

Expand Down
6 changes: 5 additions & 1 deletion R/provider-bedrock.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ method(as_json, list(ProviderBedrock, Turn)) <- function(provider, x) {
}

method(as_json, list(ProviderBedrock, ContentText)) <- function(provider, x) {
list(text = x@text)
if (is_whitespace(x@text)) {
list(text = "[empty string]")
} else {
list(text = x@text)
}
}

method(as_json, list(ProviderBedrock, ContentImageRemote)) <- function(
Expand Down
6 changes: 5 additions & 1 deletion R/provider-claude.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ method(as_json, list(ProviderClaude, Turn)) <- function(provider, x) {
}

method(as_json, list(ProviderClaude, ContentText)) <- function(provider, x) {
list(type = "text", text = x@text)
if (is_whitespace(x@text)) {
list(type = "text", text = "[empty string]")
} else {
list(type = "text", text = x@text)
}
}

method(as_json, list(ProviderClaude, ContentPDF)) <- function(provider, x) {
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ modify_list <- function(x, y) {

utils::modifyList(x, y)
}

is_whitespace <- function(x) {
grepl("^(\\s|\n)*$", x)
}
7 changes: 7 additions & 0 deletions tests/testthat/test-provider-bedrock.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ test_that("can use pdfs", {
test_pdf_local(chat_fun)
})

# Provider idiosynchronies -----------------------------------------------

test_that("continues to work after whitespace only outputs (#376)", {
chat <- chat_bedrock()
chat$chat("Respond with only two blank lines")
expect_equal(chat$chat("What's 1+1? Just give me the number"), "2")
})

# Auth --------------------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-provider-claude.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ test_that("can set beta headers", {
req <- chat_request(provider)
expect_equal(req$headers$`anthropic-beta`, c("a", "b"))
})

test_that("continues to work after whitespace only outputs (#376)", {
chat <- chat_claude()
chat$chat("Respond with only two blank lines")
expect_equal(chat$chat("What's 1+1? Just give me the number"), "2")
})
8 changes: 8 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ test_that("informative error if no key", {
expect_false(key_exists("FOO"))
expect_snapshot(key_get("FOO"), error = TRUE)
})

test_that("detects whitespace", {
expect_true(is_whitespace("\n\n\n \t"))
expect_true(is_whitespace(""))

expect_false(is_whitespace("a"))
expect_false(is_whitespace("."))
})