Skip to content
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

Format OpenAIError::APIError with client-friendly message #238

Closed
GabrielBianconi opened this issue Jun 28, 2024 · 4 comments · Fixed by #240
Closed

Format OpenAIError::APIError with client-friendly message #238

GabrielBianconi opened this issue Jun 28, 2024 · 4 comments · Fixed by #240
Labels
enhancement New feature or request

Comments

@GabrielBianconi
Copy link
Contributor

GabrielBianconi commented Jun 28, 2024

In error.rs, we have:

...
    #[error("{:?}: {}", .0.r#type, .0.message)]
    ApiError(ApiError),
...
pub struct ApiError {
    pub message: String,
    pub r#type: Option<String>,
    pub param: Option<String>,
    pub code: Option<String>,
}
...

The {:?} leads to error messages that aren't client-friendly, such as:

Some(\"invalid_request_error\"): This model's maximum context length is 16385 tokens. However, your messages resulted in 25015 tokens (24961 in the messages, 54 in the functions). Please reduce the length of the messages or functions.

Could we replace it with something more client-friendly like:

...
    #[error("{}: {}", .0.r#type. unwrap_or("unknown_error_type".to_string()), .0.message)]
    ApiError(ApiError),
...

Thanks!

@64bit
Copy link
Owner

64bit commented Jun 28, 2024

Sure, however I wouldn't put a made up type unknown_error_type and instead keep it empty '' when absent, or not even include it in format string if absent.

In addition, it makes sense to do the same for all fields of struct ApiError for consistency. PR is welcome!

@64bit 64bit added the enhancement New feature or request label Jun 28, 2024
@GabrielBianconi
Copy link
Contributor Author

Great.

@64bit Could you clarify what you have in mind for "In addition, it makes sense to do the same for all fields of struct ApiError for consistency."? Do you mean adding param and code, if available? (They're not currently included, and message isn't optional.)

I can send a PR later.

@64bit
Copy link
Owner

64bit commented Jul 2, 2024

Ah yes, your interpretation is correct. Previously the message was sort of "key": "value".
Might as well do the same for all the fields in the struct ApiError once and for all..

@GabrielBianconi
Copy link
Contributor Author

Sounds good. I'll send a PR in a few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants