feat: Add ChatCompletionsHTTPClient and support for non-streaming requests#1178
Open
copybara-service[bot] wants to merge 1 commit intomainfrom
Open
feat: Add ChatCompletionsHTTPClient and support for non-streaming requests#1178copybara-service[bot] wants to merge 1 commit intomainfrom
copybara-service[bot] wants to merge 1 commit intomainfrom
Conversation
…uests
This is part of a larger chain of commits for adding chat completion API support to the Apigee model.
The HTTP client wraps payload construction (delegating to ChatCompletionsRequest.fromLlmRequest) and response parsing (delegating to ChatCompletionsResponse.ChatCompletion / ChatCompletionChunkCollection) for both non-streaming and streaming Server-Sent Events responses.
END_PUBLIC
Key behaviors:
- Tri-state call timeout policy:
* httpOptions == null OR timeout() empty: applies a default 5-minute call
timeout to prevent indefinite hangs in the common unconfigured case.
* httpOptions.timeout() == 0: respected as the explicit caller opt-in to
infinite hang for long-running streams or batch jobs.
* httpOptions.timeout() > 0: applied directly as the call timeout.
This default intentionally diverges from the GenAI HttpOptions convention
(which treats unset as infinite) as a defensive measure since this client
does not yet have HTTP retry support.
- SSE prefix handling accepts both "data: foo" (with space) and "data:foo"
(without space) per the SSE spec, matching providers that omit the
trailing space.
- A single malformed JSON chunk in a streaming response is logged and
skipped rather than aborting the entire stream. IOException
(connection-level) still propagates as a stream error.
- Content-Type is defensively forced to application/json by replacing
rather than appending, preventing duplicate or conflicting headers if a
caller supplies their own Content-Type.
- Headers parameter accepts null (treated as no extra headers) and is
stored as an ImmutableMap for thread-safe reuse across concurrent
generateContent calls.
Test additions (16 total, +12 new):
- HTTP error status (4xx/5xx) propagation for both streaming and
non-streaming.
- Empty body propagation.
- Streaming continues past a single malformed chunk.
- SSE "data:" prefix accepted with or without trailing space.
- Custom headers reach the wire.
- Caller-supplied Content-Type is overridden, not appended.
- baseUrl with and without trailing slash.
- Constructor tri-state timeout (null, zero=infinite, positive).
- Constructor null headers parameter.
All testSubscriber.await() calls bounded to 500ms to prevent test hangs.
PiperOrigin-RevId: 902749082
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: Add ChatCompletionsHTTPClient and support for non-streaming requests
This is part of a larger chain of commits for adding chat completion API support to the Apigee model.
The HTTP client wraps payload construction (delegating to ChatCompletionsRequest.fromLlmRequest) and response parsing (delegating to ChatCompletionsResponse.ChatCompletion / ChatCompletionChunkCollection) for both non-streaming and streaming Server-Sent Events responses.
END_PUBLIC
Key behaviors:
Tri-state call timeout policy:
timeout to prevent indefinite hangs in the common unconfigured case.
infinite hang for long-running streams or batch jobs.
This default intentionally diverges from the GenAI HttpOptions convention
(which treats unset as infinite) as a defensive measure since this client
does not yet have HTTP retry support.
SSE prefix handling accepts both "data: foo" (with space) and "data:foo"
(without space) per the SSE spec, matching providers that omit the
trailing space.
A single malformed JSON chunk in a streaming response is logged and
skipped rather than aborting the entire stream. IOException
(connection-level) still propagates as a stream error.
Content-Type is defensively forced to application/json by replacing
rather than appending, preventing duplicate or conflicting headers if a
caller supplies their own Content-Type.
Headers parameter accepts null (treated as no extra headers) and is
stored as an ImmutableMap for thread-safe reuse across concurrent
generateContent calls.
Test additions (16 total, +12 new):
non-streaming.
All testSubscriber.await() calls bounded to 500ms to prevent test hangs.