Skip to content

[Search] Add knowledge base retrieve stream convenience APIs - #50210

Merged
Xiaofei Cao (XiaofeiCao) merged 15 commits into
Azure:copilot/generate-azure-search-sdk-2026-08-01-previewfrom
XiaofeiCao:search-retrieve-stream
Aug 26, 2026
Merged

[Search] Add knowledge base retrieve stream convenience APIs#50210
Xiaofei Cao (XiaofeiCao) merged 15 commits into
Azure:copilot/generate-azure-search-sdk-2026-08-01-previewfrom
XiaofeiCao:search-retrieve-stream

Conversation

@XiaofeiCao

@XiaofeiCao Xiaofei Cao (XiaofeiCao) commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Description

Adds regeneration-safe convenience APIs for knowledge-base retrieveStream on top of #49988.

Testing

Add regeneration-safe retrieveStream convenience APIs, typed knowledge-base stream events, and Search-local SSE parsing and lifecycle support.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
Remove the generated zero-argument helper that calls an overload no longer present after the synonym-map listing API update.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
34 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

private static final String RESPONSE_COMPLETED_JSON = "{\"statusCode\":200,\"response\":{}}";

@Test
public void convertsAllStreamEventVariants() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event model/wrapper deserialization tests

*/
@Generated
@ServiceMethod(returns = ReturnType.COLLECTION)
public Flux<ServerSentEvent<KnowledgeBaseRetrievalStreamEvent>>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async retrieveStream

*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public void retrieveStream(KnowledgeBaseRetrievalOptions retrievalRequest,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sync retrieveStream

Keep knowledge base stream event dispatch and payload decoding in the unexported knowledgebases implementation package while preserving generated client wiring.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
Keep the target branch's repaired synonym-map convenience method and preserve the regeneration-safe knowledge base retrieve stream implementation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
/**
* Represents a knowledge base retrieval stream event that is not recognized by this SDK version.
*/
public final class UnknownKnowledgeBaseRetrievalStreamEvent extends KnowledgeBaseRetrievalStreamEvent {

@XiaofeiCao Xiaofei Cao (XiaofeiCao) Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For unknown events, we'll not throw exception, but put them all in this UnknownKnowledgeBaseRetrievalStreamEvent.

Welcome suggestions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine. I believe the other langs are doing the same

Treat terminal predicates as inclusive early-stop signals while allowing EOF and HTTP 204 responses to complete normally without terminal events.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
Keep Search-specific knowledge base stream coverage while relying on the Azure Core SSE PR for generic parser, Flux, response, and lifecycle tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
@XiaofeiCao
Xiaofei Cao (XiaofeiCao) marked this pull request as ready for review August 24, 2026 07:35
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
34 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 adds regeneration-safe, typed server-sent events (SSE) convenience APIs for Knowledge Base retrieveStream in azure-search-documents, including Search-local SSE parsing utilities and typed wrappers for known knowledge-base events with a forward-compatible “unknown event” fallback.

Changes:

  • Introduces Search-local SSE public models (ServerSentEvent, ServerSentEventListener) and internal stream parsing/lifecycle utilities.
  • Adds typed Knowledge Base retrieval stream event wrappers + converter, and new sync/async retrieveStream convenience APIs that emit/dispatch typed SSE events.
  • Adds focused tests for stream event conversion and client integration, and updates the changelog.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
sdk/search/azure-search-documents/src/test/java/com/azure/search/documents/knowledgebases/KnowledgeBaseRetrievalStreamTests.java Adds tests validating typed event conversion and sync/async streaming behavior.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/models/ServerSentEventListener.java Adds public sync listener contract for SSE consumption.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/models/ServerSentEvent.java Adds public typed SSE event model and internal construction hook.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/models/implementation/sse/ServerSentEventStreams.java Adds facade utilities to decode/listen to SSE streams from a single response.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/models/implementation/sse/ServerSentEventStreamResponse.java Validates SSE response status/content-type and supports body cancellation.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/models/implementation/sse/ServerSentEventStream.java Implements SSE decoding and sync listener processing.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/models/implementation/sse/ServerSentEventHelper.java Internal accessor helper for constructing ServerSentEvent.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseRetrievalStreamEvent.java Adds base type for typed KB retrieval stream events + terminal flag.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/UnknownKnowledgeBaseRetrievalStreamEvent.java Adds raw-data fallback model for unknown event names.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseRetrievalStartedStreamEvent.java Adds typed wrapper for retrieval.started.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseActivityStartedStreamEvent.java Adds typed wrapper for activity.started.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseActivityCompletedStreamEvent.java Adds typed wrapper for activity.completed.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseAnswerCompletedStreamEvent.java Adds typed wrapper for answer.completed.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseReferencesCompletedStreamEvent.java Adds typed wrapper for references.completed.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseErrorStreamEvent.java Adds typed terminal wrapper for error.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/models/KnowledgeBaseResponseCompletedStreamEvent.java Adds typed terminal wrapper for response.completed.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/implementation/KnowledgeBaseRetrievalStreamEventConverter.java Adds event-name-to-typed-model conversion logic.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/KnowledgeBaseRetrievalClient.java Adds new public sync retrieveStream(..., listener) convenience API.
sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/knowledgebases/KnowledgeBaseRetrievalAsyncClient.java Adds new public async retrieveStream(...) convenience API returning Flux.
sdk/search/azure-search-documents/customizations/src/main/java/SearchCustomizations.java Injects regeneration-safe additions for SSE + KB streaming APIs/models.
sdk/search/azure-search-documents/CHANGELOG.md Documents the newly added typed SSE streaming APIs and models.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: eecc64df-f690-4410-b68c-b45e323ec918
Co-authored-by: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com>
Move stream models and implementation out of embedded customization source, delegate generated client APIs through a typed utility, and mark custom model members as generated and immutable where appropriate.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d61a8857-b536-4f72-ae34-6f532842dec8
Remove the dedicated stream utility and keep authorization, conversion, and terminal-event wiring directly in the generated sync and async stream APIs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d61a8857-b536-4f72-ae34-6f532842dec8
Restore the reviewed ReadValueCallback usage with the correct import and generic order, and remove the redundant local EventReader interface.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d61a8857-b536-4f72-ae34-6f532842dec8
Generate the custom server-sent event and knowledge base stream event models from SearchCustomizations so cleanup-enabled TypeSpec regeneration preserves their @generated members. Keep parser and converter implementation sources handwritten.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d61a8857-b536-4f72-ae34-6f532842dec8
Remove ServerSentEvent and ServerSentEventListener from SearchCustomizations and remove their @generated annotations so they remain handwritten pending migration to azure-core.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d61a8857-b536-4f72-ae34-6f532842dec8

private void appendByte(byte value) {
if (lineLength == lineBytes.length) {
lineBytes = Arrays.copyOf(lineBytes, lineBytes.length * 2);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agent feedback — for consideration only: ServerSentEventDecoder retains an entire event until a blank line dispatches it. Both the current line and accumulated data: lines are unbounded, so a malformed endpoint could cause excessive client memory use.

Would it be worth enforcing a maximum pending-event size? One possible starting point is 16 MiB of raw UTF-8, subject to confirmation against the service's largest legitimate serialized event, especially references.completed. A single counter for all non-delimiter bytes since the previous blank line would cover both one oversized line and many smaller data: lines. It could be checked before expanding lineBytes and reset after blank-line dispatch.

If adopted, useful boundary cases would include an over-limit unterminated line, aggregate data: lines over the limit, an event exactly at the limit, and resetting the counter between events.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible implementation shape:

private static final int MAX_PENDING_EVENT_BYTES = 16 * 1024 * 1024;

private int pendingEventBytes;

private void appendByte(byte value) {
    if (pendingEventBytes == MAX_PENDING_EVENT_BYTES) {
        throw new IllegalStateException(
            "Server-sent event exceeded the maximum size of "
                + MAX_PENDING_EVENT_BYTES + " bytes.");
    }

    pendingEventBytes++;

    if (lineLength == lineBytes.length) {
        int newLength = Math.min(lineBytes.length * 2, MAX_PENDING_EVENT_BYTES);
        lineBytes = Arrays.copyOf(lineBytes, newLength);
    }

    lineBytes[lineLength++] = value;
}

Then reset pendingEventBytes when processLine receives the blank line that dispatches the pending event. Checking before buffer growth also avoids integer overflow or allocating beyond the configured ceiling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16mb sounds reasonable to me. efrainretana let us know if this is not suitable for retrieval response.

Cap retained SSE field content at 16 MiB per pending event and fail before growing the line buffer beyond the limit. Document the behavior and cover single-line, aggregate, exact-boundary, reset, and listener error cases.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 339a216b-0c44-402a-b89e-2e26ea2d30ce
@XiaofeiCao
Xiaofei Cao (XiaofeiCao) merged commit 5163d3d into Azure:copilot/generate-azure-search-sdk-2026-08-01-preview Aug 26, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants