Skip to content
Draft
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 R/provider-claude.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,17 @@ method(chat_body, ProviderAnthropic) <- function(
}))

if (!is.null(type)) {
# Build description based on whether the type is required
if (type@required) {
description <- "Extract structured data from the conversation."
} else {
description <- "Extract structured data from the conversation. The data parameter is optional - if you cannot find the requested information in the conversation, do not include the data parameter in your tool call at all and instead return an empty object {}."
}

tool_def <- ToolDef(
function(...) {},
name = "_structured_tool_call",
description = "Extract structured data",
description = description,
arguments = type_object(data = type)
)
tools[[tool_def@name]] <- tool_def
Expand Down
42 changes: 42 additions & 0 deletions tests/testthat/_vcr/anthropic-optional-array.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions tests/testthat/_vcr/anthropic-optional-types.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 44 additions & 44 deletions tests/testthat/_vcr/anthropic-structured-data.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions tests/testthat/test-provider-claude.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,56 @@ test_that("batch chat works", {
)
expect_equal(out, c("Des Moines", "Albany", "Sacramento", "Austin"))
})

test_that("optional types return NULL when data unavailable", {
vcr::local_cassette("anthropic-optional-types")

prompt <- "
# Apples are tasty
By Hadley Wickham

Apples are delicious and tasty and I like to eat them.
"

chat <- chat_anthropic_test()
result <- chat$chat_structured(
prompt,
type = type_string("The publication year", required = FALSE)
)
expect_null(result)

chat2 <- chat_anthropic_test()
result2 <- chat2$chat_structured(
prompt,
type = type_integer("Number of citations", required = FALSE)
)
expect_null(result2)
})

test_that("optional fields in arrays become NA when missing", {
vcr::local_cassette("anthropic-optional-array")

prompt <- "
Apples are tasty.
Oranges are orange in colour.
Bananas are nutritious, and their colour is yellow.
"

chat <- chat_anthropic_test()
result <- chat$chat_structured(
prompt,
type = type_array(
type_object(
name = type_string("Name of the fruit", required = FALSE),
adjective = type_string("Descriptive adjective", required = FALSE),
colour = type_string("Colour of the fruit", required = FALSE)
)
)
)

expect_s3_class(result, "data.frame")
expect_equal(nrow(result), 3)
expect_equal(result$name, c("Apples", "Oranges", "Bananas"))
expect_equal(result$adjective, c("tasty", "", "nutritious"))
expect_equal(result$colour, c("", "orange", "yellow"))
})
Loading