Skip to content

fix(discoveryengine): change streamAnswer return type to array#3939

Open
AmariahAK wants to merge 1 commit into
googleapis:mainfrom
AmariahAK:main
Open

fix(discoveryengine): change streamAnswer return type to array#3939
AmariahAK wants to merge 1 commit into
googleapis:mainfrom
AmariahAK:main

Conversation

@AmariahAK

Copy link
Copy Markdown

Description

The streamAnswer REST endpoint returns a JSON array of
AnswerQueryResponse frames at runtime, but the generated TypeScript types
declared a single GoogleCloudDiscoveryengineV1AnswerQueryResponse object.
This caused res.data.answerQueryToken, res.data.answer?.relatedQuestions,
and similar fields to be undefined at runtime while TypeScript reported
them as present — because res.data is actually an array, not an object.

Root cause

The discovery document's response.$ref points to a single
AnswerQueryResponse, but the actual REST HTTP response body is a JSON
array. The generator faithfully produces the single-object type from the
discovery doc, which doesn't match reality. The discovery doc description
even says "returns multiple AnswerQueryResponse messages in a stream"
the $ref just wasn't declared as an array.

Changes

  • src/apis/discoveryengine/v1.ts — Changed all streamAnswer
    method signatures and createAPIRequest calls from
    Schema$GoogleCloudDiscoveryengineV1AnswerQueryResponse to
    Schema$GoogleCloudDiscoveryengineV1AnswerQueryResponse[]
    (33 occurrences, 3 resource paths)

  • src/apis/discoveryengine/v1beta.ts — Same changes with V1beta
    prefix (33 occurrences, 3 resource paths)

  • src/apis/discoveryengine/v1alpha.ts — Same changes with V1alpha
    prefix (33 occurrences, 3 resource paths)

  • src/generator/templates/method-partial.njk — Added detection
    for :stream in the method path. When the path contains :stream
    (Google's REST convention for streaming custom methods like
    :streamAnswer, :streamGenerate, etc.), the generated response type
    is automatically array-ified to prevent this bug from recurring on
    the next nightly regeneration.

Non-streaming answer methods (which correctly return a single object)
are not affected — only streamAnswer methods were changed.

Impact

  • TypeScript users of @googleapis/discoveryengine can now access
    res.data[0].answerQueryToken, res.data.filter(...), etc. without
    undefined surprises or @ts-expect-error casts.
  • No runtime behavior change — the REST endpoint already returned
    an array; this fix only corrects the TypeScript types to match.
  • Generator fix prevents the same class of bug for any future
    :stream* methods across all APIs.

Testing

  • Verified 33 occurrences of [] per version file (v1/v1beta/v1alpha),
    99 total across all streamAnswer method signatures and
    createAPIRequest calls
  • Confirmed 6 answer (non-stream) method createAPIRequest calls
    remain correctly typed as single objects — no accidental changes
  • Confirmed only 4 files touched, no other APIs affected
  • npm run compile passes (9 pre-existing TypeScript errors in
    unrelated drive API test files exist before and after this change)

Additional Information

The discovery document itself should also be updated upstream so the
response.$ref correctly declares an array. The generator template
change in this PR acts as a safety net until that happens.

Fixes #3930 🦕


---

The top template block in your paste (with the bullet points about `streamAnswer`) would go above the checklist, roughly like:

```markdown
streamAnswer REST endpoint returns a JSON array of AnswerQueryResponse
frames at runtime, but the generated TypeScript types declared a single
object, causing res.data.answerQueryToken and similar fields to be
undefined at runtime.

- Fix v1, v1beta, and v1alpha generated types (99 total)
- Add :stream path detection in method-partial.njk generator template
  to prevent regression

Fixes #3930

Then fill the checklist as:

- [x] Make sure to open an issue as a bug/issue before writing your code
- [x] Ensure the tests and linter pass (see Testing section)
- [ ] Code coverage does not decrease (no runtime behavior change)
- [x] Appropriate docs were updated (inline JSDoc unchanged as it already
  described streaming behavior)
- [ ] Appropriate comments were added (N/A — type-only fix)
- [x] No new warnings or issues will be generated from this change

Fixes #3930 🦕

streamAnswer REST endpoint returns a JSON array of
AnswerQueryResponse frames at runtime, but the generated
TypeScript types declared a single object, causing
res.data.answerQueryToken and similar fields to be
undefined at runtime.

- Fix v1, v1beta, and v1alpha generated types (99 total)
- Add :stream path detection in method-partial.njk
  generator template to prevent regression

Fixes googleapis#3930

Co-authored-by: atlarix-agent <agent@atlarix.dev>
@AmariahAK
AmariahAK requested a review from a team as a code owner July 18, 2026 12:09
@product-auto-label product-auto-label Bot added size: m Pull request size is medium. api: discoveryengine Issues related to the Discovery Engine API API. labels Jul 18, 2026
@google-cla

google-cla Bot commented Jul 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: discoveryengine Issues related to the Discovery Engine API API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Discovery Engine] streamAnswer is typed as one AnswerQueryResponse, but REST returns frame arrays

1 participant