Skip to content

feat(developer-knowledge): add Developer Knowledge API samples - #4424

Open
llatif wants to merge 5 commits into
GoogleCloudPlatform:mainfrom
llatif:feat/developerknowledge-samples
Open

feat(developer-knowledge): add Developer Knowledge API samples#4424
llatif wants to merge 5 commits into
GoogleCloudPlatform:mainfrom
llatif:feat/developerknowledge-samples

Conversation

@llatif

@llatif llatif commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Description

Adds Node.js code samples for the Developer Knowledge API using the @google/developer-knowledge client library:

  • answerQuery.js (developerknowledge_answer_query) - Answers queries against a developer knowledge corpus.
  • getDocument.js (developerknowledge_get_document) - Retrieves a document by resource name.
  • batchGetDocuments.js (developerknowledge_batch_get_documents) - Retrieves multiple documents in a single request.
  • searchDocumentChunks.js (developerknowledge_search_document_chunks) - Searches document chunks with pagination limit.

Includes comprehensive Mocha unit tests (test/samples.test.js), ESLint/Prettier configuration with license headers, package metadata, README documentation, and added developer-knowledge to CODEOWNERS.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • Required CI tests pass (see CI testing)
  • These samples need a new API enabled in testing projects to pass (developerknowledge.googleapis.com)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

llatif added 2 commits August 25, 2026 18:35
Add standalone code samples and test suites for Developer Knowledge API:
- searchDocumentChunks
- getDocument
- batchGetDocuments
- answerQuery
@llatif
llatif requested review from a team as code owners August 26, 2026 02:12
@product-auto-label product-auto-label Bot added the samples Issues that are directly related to samples. label Aug 26, 2026
@snippet-bot

snippet-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 4 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@llatif
llatif marked this pull request as draft August 26, 2026 02:13

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces Node.js code samples and tests for the Google Developer Knowledge API, including scripts for answering queries, retrieving documents, batch-fetching documents, and searching document chunks. The review feedback highlights potential TypeError issues across several files (answerQuery.js, getDocument.js, and searchDocumentChunks.js) where properties or methods are accessed on potentially undefined or null fields (such as response.answer, document.content, and chunk.content). It is recommended to use optional chaining or fallback values to ensure robust error handling.

Comment on lines +36 to +42
console.log(`Answer:\n${response.answer.answerText}\n`);
const citationsCount = response.answer.citations
? response.answer.citations.length
: 0;
const referencesCount = response.answer.references
? response.answer.references.length
: 0;

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.

medium

If response.answer is undefined or null (for example, if no answer could be generated for the query), accessing response.answer.answerText or its other properties directly will throw a TypeError. Use optional chaining or default values to safely handle cases where the answer is missing.

Suggested change
console.log(`Answer:\n${response.answer.answerText}\n`);
const citationsCount = response.answer.citations
? response.answer.citations.length
: 0;
const referencesCount = response.answer.references
? response.answer.references.length
: 0;
const answerText = response.answer?.answerText || '';
console.log('Answer:\n' + answerText + '\n');
const citationsCount = response.answer?.citations?.length || 0;
const referencesCount = response.answer?.references?.length || 0;

console.log(`URI: ${document.uri}`);
console.log(`Data Source: ${document.dataSource}`);
console.log(`Content Length: ${document.contentLengthBytes} bytes`);
console.log(`Content Preview: ${document.content.substring(0, 150)}...\n`);

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.

medium

If document.content is undefined or null, calling .substring() on it will throw a TypeError. Use a fallback empty string or optional chaining to safely handle missing content.

Suggested change
console.log(`Content Preview: ${document.content.substring(0, 150)}...\n`);
const contentPreview = (document.content || '').substring(0, 150);
console.log('Content Preview: ' + contentPreview + '...\n');

for (const chunk of chunks) {
console.log(`Parent Document: ${chunk.parent}`);
console.log(`Chunk ID: ${chunk.id}`);
console.log(`Content Preview: ${chunk.content.substring(0, 100)}...\n`);

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.

medium

If chunk.content is undefined or null, calling .substring() on it will throw a TypeError. Use a fallback empty string or optional chaining to safely handle missing content.

    const contentPreview = (chunk.content || '').substring(0, 100);
    console.log('Content Preview: ' + contentPreview + '...\n');

@llatif
llatif marked this pull request as ready for review August 26, 2026 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant