r-lib/httr2

`req_perform_stream(mode="text")` error during error handling

Closed this issue · 0 comments

library(httr2)

req <- request("https://api.openai.com/v1") |>
  req_auth_bearer_token("bad-openai-key") |>
  req_retry(max_tries = 2) |>
  req_error(body = function(resp) {
    httr2::resp_body_json(resp)$error$message
  }) |>
  req_url_path_append("/chat/completions") |>
  req_body_json(list(list(role = "user", content = "Why is the sky blue?"))) |>
  req_perform_connection(mode = "text")

Results in:

Error in `readBin()`:
! can only read from a binary connection

rather than the expected:

Error in `req_perform_connection()`:
! HTTP 401 Unauthorized.
• Incorrect API key provided: bad-open**-key. You can find your API key at [https://platform.openai.com/account/api-keys.](vscode-file://vscode-app/Applications/Positron.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html#)

This is due to the read_con call in req_perform_connection, it's expecting a binary connection instead of a text connection.