-
Notifications
You must be signed in to change notification settings - Fork 114
Handle invalid JSON in GitHub Models 429 responses #902
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
32a0c07
bf28d55
2f6b671
d8675ce
eb46b93
2ee2ebd
045ba09
6d613a0
8e0de07
e2a5318
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,18 +153,35 @@ method(base_request_error, ProviderOpenAICompatible) <- function( | |
| req | ||
| ) { | ||
| req_error(req, body = function(resp) { | ||
| if (resp_content_type(resp) == "application/json") { | ||
| error <- resp_body_json(resp)$error | ||
| if (is_string(error)) { | ||
| error | ||
| } else if (is.list(error)) { | ||
| error$message | ||
| } else { | ||
| prettify(resp_body_string(resp)) | ||
| # Default to treating as text | ||
| is_json <- FALSE | ||
| try( | ||
| { | ||
| if (resp_content_type(resp) == "application/json") { | ||
| is_json <- TRUE | ||
| } | ||
| }, | ||
| silent = TRUE | ||
| ) | ||
D-M4rk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (is_json) { | ||
| # Try parsing JSON, but fall back to text if it fails (e.g. 429 text body) | ||
| body <- tryCatch(resp_body_json(resp), error = function(e) NULL) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest making a is_json <- function(text) {
tryCatch({
jsonlite::read_json(text)
TRUE
}), function(err) FALSE
}Then alway use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated identical, for is_json() helper approach—would you like me to implement that as well, or is the current tryCatch around resp_body_json() sufficient for now? |
||
|
|
||
| if (!is.null(body)) { | ||
| error <- body$error | ||
| if (is_string(error)) { | ||
| return(error) | ||
| } else if (is.list(error)) { | ||
| return(error$message) | ||
| } else { | ||
| return(prettify(resp_body_string(resp))) | ||
| } | ||
| } | ||
| } else if (resp_content_type(resp) == "text/plain") { | ||
| resp_body_string(resp) | ||
| } | ||
|
|
||
| # Fallback for text/plain, other types, or failed JSON parsing | ||
| resp_body_string(resp) | ||
| }) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.