-
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?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a crash in OpenAI-compatible providers (including chat_github()) when APIs return HTTP 429 rate limit errors with malformed JSON bodies but application/json content-type headers. Previously, the code would crash during JSON parsing before httr2's retry mechanism could handle the 429 status.
Key changes:
- Added graceful JSON parsing error handling using
tryCatchinbase_request_error - Falls back to plain text response bodies when JSON parsing fails
- Updated NEWS.md to document the fix
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| R/provider-openai-compatible.R | Added error handling to gracefully handle invalid JSON in error responses with JSON content-type |
| NEWS.md | Documented the fix for handling malformed 429 responses |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
|
||
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest making a is_json() function like:
is_json <- function(text) {
tryCatch({
jsonlite::read_json(text)
TRUE
}), function(err) FALSE
}Then alway use resp_string_string()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
json response na handling
Closes #901
Description
This PR fixes a crash in
chat_github(and other OpenAI-compatible providers) when the API returns a 429 rate limit error with a text body but aContent-Type: application/jsonheader.Previously,
ellmerwould crash with a JSON parsing error (lexical error: invalid char in json text) beforehttr2could detect the 429 status and trigger a retry.Changes
base_request_errorinR/provider-openai-compatible.R.resp_body_json()intryCatchto handle invalid JSON gracefully.resp_body_string()if parsing fails, ensuring the error propagates correctly tohttr2's retry mechanism.