Add initial chat completions client - #1
faheemKamboh wants to merge 28 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2014bff7e3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| request.body = JSON.generate(body) | ||
|
|
||
| response = perform_request(uri, request) | ||
| parsed_body = parse_response(response) |
There was a problem hiding this comment.
Handle non-JSON error responses by status
If Z.ai or an intermediary returns a non-empty HTML/text body for a 401, 429, or 5xx response, this parses the body before checking the HTTP status, and the JSON::ParserError rescue raises ZAI::ParseError instead of the intended AuthenticationError, RateLimitError, or ServerError with status/headers. That prevents callers from reliably handling retry/authentication cases unless every error response is valid JSON.
Useful? React with 👍 / 👎.
| # frozen_string_literal: true | ||
|
|
||
| module ZAi | ||
| module ZAI |
There was a problem hiding this comment.
Update the packaged RBS namespace
Renaming the runtime namespace to ZAI here leaves the packaged type signature stale: sig/z_ai.rbs still declares module ZAi and does not describe ZAI::Client or the new response/error classes. Because the gemspec still includes sig/, typed consumers of the released gem will be told the new public API does not exist while the old constant does, so the RBS should be updated or removed with this rename.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e42e2de12d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def api_error_message(body) | ||
| return unless body.is_a?(Hash) | ||
|
|
||
| body.dig("error", "message") || body["message"] || body["msg"] |
There was a problem hiding this comment.
Handle string error bodies without TypeError
If a non-2xx JSON response has a top-level error value that is not an object, such as a gateway/path 404 body like { "error": "Not Found" }, this dig call raises TypeError before api_error_class can raise APIError with the status, body, and headers. Callers then cannot handle the failure through the SDK error hierarchy even though the response was valid JSON, so guard the error value's type or handle string errors before digging.
Useful? React with 👍 / 👎.
Summary
ZAI::Clientwith chat completions support.ZAitoZAIbefore the gem has public usage.Scope intentionally deferred
Notes
This is intentionally small and dependency-free. Specs use RSpec doubles for the
Net::HTTPboundary instead of adding a test-only HTTP mocking dependency in the first PR.The client targets Z.ai's documented general API endpoint and
/chat/completionspath.Validation