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
11 changes: 11 additions & 0 deletions R/chat-structured.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ extract_data <- function(turn, type, convert = TRUE, needs_wrapper = FALSE) {
out
}

# Tool-based structured output can't stream (#977)
uses_tool_structured_output <- function(provider, type) {
if (S7_inherits(provider, ProviderAWSBedrock)) {
TRUE
} else if (S7_inherits(provider, ProviderAnthropic)) {
!has_claude_structured_output(provider@model)
} else {
FALSE
}
}

# OpenAI only works with objects, so we wrap and unwrap as needed
type_needs_wrapper <- function(type, provider) {
S7_inherits(provider, ProviderOpenAICompatible) &&
Expand Down
10 changes: 8 additions & 2 deletions R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ Chat <- R6::R6Class(
needs_wrapper <- type_needs_wrapper(type, private$provider)
type <- wrap_type_if_needed(type, needs_wrapper)

stream <- echo != "none" &&
!uses_tool_structured_output(private$provider, type)

coro::collect(private$submit_turns(
turn,
type = type,
stream = echo != "none",
stream = stream,
echo = echo,
controller = stream_controller()
))
Expand Down Expand Up @@ -280,10 +283,13 @@ Chat <- R6::R6Class(
needs_wrapper <- type_needs_wrapper(type, private$provider)
type <- wrap_type_if_needed(type, needs_wrapper)

stream <- echo != "none" &&
!uses_tool_structured_output(private$provider, type)

done <- coro::async_collect(private$submit_turns_async(
turn,
type = type,
stream = echo != "none",
stream = stream,
echo = echo,
controller = stream_controller()
))
Expand Down
1 change: 0 additions & 1 deletion R/provider-aws.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ method(chat_request, ProviderAWSBedrock) <- function(
)
tools[[tool_def@name]] <- tool_def
tool_choice <- list(tool = list(name = tool_def@name))
stream <- FALSE
} else {
tool_choice <- NULL
}
Expand Down
1 change: 0 additions & 1 deletion R/provider-claude.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ method(chat_body, ProviderAnthropic) <- function(
)
tools[[tool_def@name]] <- tool_def
tool_choice <- list(type = "tool", name = tool_def@name)
stream <- FALSE
output_config <- NULL
}
} else {
Expand Down
Loading