diff --git a/R/provider-openai.R b/R/provider-openai.R index dc9afec13..456249132 100644 --- a/R/provider-openai.R +++ b/R/provider-openai.R @@ -323,7 +323,8 @@ method(value_turn, ProviderOpenAI) <- function( }) tokens <- value_tokens(provider, result) - cost <- get_token_cost(provider, tokens, variant = result$service_tier) + variant <- result$service_tier %||% "default" + cost <- get_token_cost(provider, tokens, variant = variant) AssistantTurn( contents = contents, json = result, diff --git a/tests/testthat/test-provider-openai.R b/tests/testthat/test-provider-openai.R index 2668428c8..44bdd37b2 100644 --- a/tests/testthat/test-provider-openai.R +++ b/tests/testthat/test-provider-openai.R @@ -142,3 +142,20 @@ test_that("can extract dummy response from malformed JSON", { list(custom_id = "123", response = list(status_code = 500)) ) }) + +test_that("value_turn handles NULL service_tier gracefully", { + provider <- chat_openai_test()$get_provider() + + result <- list( + output = list( + list(type = "message", content = list(list(text = "Hello"))) + ), + usage = NULL, + service_tier = NULL + ) + + turn <- value_turn(provider, result) + expect_s7_class(turn, AssistantTurn) + expect_equal(turn@tokens[["input"]], 0) + expect_equal(turn@tokens[["output"]], 0) +})