Skip to content

Fixes #4930: cap Titan Bedrock embedding input to avoid ValidationException (#29920) - #30021

Merged
mohityadav766 merged 1 commit into
1.13from
cherry-pick-29920-1.13
Jul 14, 2026
Merged

mohityadav766 merged 1 commit into
1.13from
cherry-pick-29920-1.13

Conversation

@mohityadav766

@mohityadav766 mohityadav766 commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

Backport of #29920 to 1.13.

Problem

Titan text-embedding models accept at most 8192 input tokens and expose no request-side truncate directive, so oversized chunks were rejected by Bedrock with a ValidationException.

Fix

  • Cap input at 16384 chars up front (StringUtils.truncate) before it reaches Titan's inputText.
  • On a token-limit ValidationException, halve the already-capped input and retry (up to 3 times). Detection is a typed ValidationException check plus an "input token" message marker, so throttling/auth/other validation errors are never retried.
  • Halving is empty-safe and surrogate-pair-safe, so the emoji/CJK path can't split a code point.

The char cap alone can't bound token-dense scripts (CJK, emoji) where chars ≈ tokens; the retry gives a content-agnostic guarantee.

Adaptation from the original

This is not a clean cherry-pick. The upstream change sits on top of an embedding-family refactor that never landed in 1.13: main's BedrockEmbeddingClient has a BedrockEmbeddingFamily enum (Titan V1/V2 + Cohere), a doEmbedQuery path, and per-family request builders. 1.13's client is Titan-only with a single inline doEmbed.

So the fix is applied to the Titan-only shape that exists on this branch. The family/Cohere portions of the upstream change — and their tests (familyFor, validateDimension, Cohere request/response shapes) — are omitted, since none of that code exists here.

Tests

Kept the 8 upstream tests covering the cap and the retry loop, plus a Titan response-parse test. All 9 pass:

mvn -pl openmetadata-service -Dtest=BedrockEmbeddingClientTest test
Tests run: 9, Failures: 0, Errors: 0, Skipped: 0

Note (pre-existing, not addressed here)

1.13 always sends dimensions in the Titan payload, which amazon.titan-embed-text-v1 does not accept. That's a pre-existing 1.13 bug unrelated to #4930 — the family refactor is what fixed it on main — and it's left alone in this backport.

🤖 Generated with Claude Code

Greptile Summary

This PR makes Bedrock Titan embedding requests handle oversized input before sending them to AWS. The main changes are:

  • Adds a 16384-character cap for Titan inputText.
  • Retries token-limit ValidationExceptions with halved input.
  • Extracts Bedrock request and response helpers for testing.
  • Adds unit tests for capping, retry exhaustion, and Titan response parsing.

Confidence Score: 4/5

The Bedrock retry flow is close, but the initial input cap can send malformed Unicode and null input can reach AWS.

  • Token-limit retry handling and attempt counting look correct for Titan messages.
  • The first truncation path is not surrogate-safe.
  • Null input is not rejected before the Bedrock request is built.

openmetadata-service/src/main/java/org/openmetadata/service/search/vector/client/BedrockEmbeddingClient.java

Important Files Changed

Filename Overview
openmetadata-service/src/main/java/org/openmetadata/service/search/vector/client/BedrockEmbeddingClient.java Adds input capping and retry logic for Bedrock embeddings; the initial cap still needs safer handling for surrogate and null input.
openmetadata-service/src/test/java/org/openmetadata/service/search/vector/client/BedrockEmbeddingClientTest.java Adds focused tests for Titan request capping, retry behavior, halving, and response parsing, but misses the first-cap surrogate boundary.

Reviews (1): Last reviewed commit: "Fixes #4930: cap Titan Bedrock embedding..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

Context used:

  • Context used - CLAUDE.md (source)

…eption (#29920)

Backport of #29920 to 1.13.

Titan text-embedding models accept at most 8192 input tokens and expose no
request-side truncate directive, so oversized chunks were rejected by Bedrock
with a ValidationException. Cap the input at 16384 chars up front, and on a
token-limit ValidationException halve the (already capped) input and retry —
a content-agnostic guarantee the char cap alone cannot provide for token-dense
scripts (CJK, emoji).

Adapted from the original: 1.13's BedrockEmbeddingClient predates the
embedding-family refactor (Cohere support, doEmbedQuery, per-family request
builders), so the fix is applied to the Titan-only shape that exists here.
The family/Cohere portions of the upstream change and its tests are omitted.

(cherry picked from commit 86909c4)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Jul 14, 2026
Comment on lines +151 to +153
static String cap(String text) {
return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: cap() can split a surrogate pair at the 16384-char boundary

cap() uses StringUtils.truncate(text, 16384) which does a plain substring; if a high surrogate lands at index 16383, the pair is split and a lone surrogate is sent in inputText. The PR carefully guards against this in halveInput but not in cap, so the emoji/CJK safety claim doesn't hold for the initial cap. Impact is low (lone surrogate at an exact boundary, serialized as an invalid escape), but for consistency trim a trailing high surrogate after truncation, e.g. String out = StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS); if (!out.isEmpty() && Character.isHighSurrogate(out.charAt(out.length()-1))) out = out.substring(0, out.length()-1); return out;.

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 0 resolved / 1 findings

Backports input capping and retry logic for Titan Bedrock embeddings to prevent ValidationExceptions, though the cap() function remains susceptible to splitting surrogate pairs at the 16384-character boundary.

💡 Edge Case: cap() can split a surrogate pair at the 16384-char boundary

📄 openmetadata-service/src/main/java/org/openmetadata/service/search/vector/client/BedrockEmbeddingClient.java:151-153

cap() uses StringUtils.truncate(text, 16384) which does a plain substring; if a high surrogate lands at index 16383, the pair is split and a lone surrogate is sent in inputText. The PR carefully guards against this in halveInput but not in cap, so the emoji/CJK safety claim doesn't hold for the initial cap. Impact is low (lone surrogate at an exact boundary, serialized as an invalid escape), but for consistency trim a trailing high surrogate after truncation, e.g. String out = StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS); if (!out.isEmpty() && Character.isHighSurrogate(out.charAt(out.length()-1))) out = out.substring(0, out.length()-1); return out;.

🤖 Prompt for agents
Code Review: Backports input capping and retry logic for Titan Bedrock embeddings to prevent ValidationExceptions, though the cap() function remains susceptible to splitting surrogate pairs at the 16384-character boundary.

1. 💡 Edge Case: cap() can split a surrogate pair at the 16384-char boundary
   Files: openmetadata-service/src/main/java/org/openmetadata/service/search/vector/client/BedrockEmbeddingClient.java:151-153

   `cap()` uses `StringUtils.truncate(text, 16384)` which does a plain `substring`; if a high surrogate lands at index 16383, the pair is split and a lone surrogate is sent in `inputText`. The PR carefully guards against this in `halveInput` but not in `cap`, so the emoji/CJK safety claim doesn't hold for the initial cap. Impact is low (lone surrogate at an exact boundary, serialized as an invalid escape), but for consistency trim a trailing high surrogate after truncation, e.g. `String out = StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS); if (!out.isEmpty() && Character.isHighSurrogate(out.charAt(out.length()-1))) out = out.substring(0, out.length()-1); return out;`.

Options

Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

Comment on lines +151 to +153
static String cap(String text) {
return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
}

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.

P1 Initial Cap Splits Surrogates

When oversized text has an emoji or other non-BMP character across the 16384-char boundary, StringUtils.truncate can leave inputText ending with a lone surrogate. The later retry halving is surrogate-safe, but this first request is already malformed and can be rejected or embedded with corrupted text.

Suggested change
static String cap(String text) {
return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
}
static String cap(String text) {
String truncated = StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
if (truncated != null
&& truncated.length() < text.length()
&& Character.isHighSurrogate(truncated.charAt(truncated.length() - 1))) {
truncated = truncated.substring(0, truncated.length() - 1);
}
return truncated;
}

Context Used: CLAUDE.md (source)

Comment on lines +151 to +153
static String cap(String text) {
return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
}

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.

P2 Null Input Reaches Bedrock

If a caller passes null through EmbeddingClient.embed, StringUtils.truncate returns null and buildRequestBody sends {"inputText":null} to Bedrock. The sibling clients reject null or blank input locally, but this path spends an SDK call and returns a generic AWS service failure instead of the client-side input error callers expect.

Suggested change
static String cap(String text) {
return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
}
static String cap(String text) {
if (text == null || text.isBlank()) {
throw new IllegalArgumentException("Text must not be null or blank");
}
String truncated = StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS);
if (truncated.length() < text.length()
&& Character.isHighSurrogate(truncated.charAt(truncated.length() - 1))) {
truncated = truncated.substring(0, truncated.length() - 1);
}
return truncated;
}

Context Used: CLAUDE.md (source)

@github-actions

Copy link
Copy Markdown
Contributor

The Python checkstyle failed.

Please run make py_format and py_format_check in the root of your repository and commit the changes to this PR.
You can also use pre-commit to automate the Python code formatting.

You can install the pre-commit hooks with make install_test precommit_install.

@mohityadav766
mohityadav766 merged commit 0b56f9a into 1.13 Jul 14, 2026
42 of 62 checks passed
@mohityadav766
mohityadav766 deleted the cherry-pick-29920-1.13 branch July 14, 2026 07:36
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

🔴 Playwright Results — 1 failure(s), 16 flaky

✅ 3961 passed · ❌ 1 failed · 🟡 16 flaky · ⏭️ 30 skipped

Shard Passed Failed Flaky Skipped
✅ Shard 1 291 0 0 4
✅ Shard 2 0 0 0 0
🟡 Shard 3 735 0 2 7
🔴 Shard 4 776 1 5 2
🟡 Shard 5 754 0 3 9
✅ Shard 6 693 0 0 0
🟡 Shard 7 712 0 6 8

Genuine Failures (failed on all attempts)

❌ Features/Table.spec.ts › Table filter with sorting should work (shard 4)
�[31mTest timeout of 60000ms exceeded.�[39m
🟡 16 flaky test(s) (passed on retry)
  • Features/BulkEditEntity.spec.ts › Database Schema (shard 3, 1 retry)
  • Features/Glossary/GlossaryWorkflow.spec.ts › should display correct status badge color and icon (shard 3, 1 retry)
  • Features/Permissions/GlossaryPermissions.spec.ts › Team-based permissions work correctly (shard 4, 1 retry)
  • Flow/PersonaDeletionUserProfile.spec.ts › User profile loads correctly before and after persona deletion (shard 4, 1 retry)
  • Flow/PersonaFlow.spec.ts › Set default persona for team should work properly (shard 4, 1 retry)
  • Flow/PlatformLineage.spec.ts › Verify Platform Lineage View (shard 4, 2 retries)
  • Pages/CustomProperties.spec.ts › Sql Query (shard 4, 1 retry)
  • Pages/CustomProperties.spec.ts › Email (shard 5, 1 retry)
  • Pages/DataContracts.spec.ts › Contract Status badge should be visible on condition if Contract Tab is present/hidden by Persona (shard 5, 1 retry)
  • Pages/DataContractsSemanticRules.spec.ts › Validate Description Rule Is_Not_Set (shard 5, 1 retry)
  • Pages/Glossary.spec.ts › Column dropdown drag-and-drop functionality for Glossary Terms table (shard 7, 1 retry)
  • Pages/Lineage/LineageFilters.spec.ts › Verify lineage schema filter selection (shard 7, 1 retry)
  • Pages/Lineage/LineageRightPanel.spec.ts › Verify custom properties tab IS visible for supported type: searchIndex (shard 7, 1 retry)
  • Pages/TestSuite.spec.ts › Test suite tab switching keeps active bundle suite data after stale table suite response (shard 7, 1 retry)
  • Pages/UserDetails.spec.ts › Create team with domain and verify visibility of inherited domain in user profile after team removal (shard 7, 1 retry)
  • Pages/Users.spec.ts › Permissions for table details page for Data Consumer (shard 7, 1 retry)

📦 Download artifacts

How to debug locally
# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip    # view trace

This branch had an error being deployed

1 failed deployment
test — 35a962f7 Deployed Jul 14, 2026 by mohityadav766 via playwright-ci-postgresql (2, 7) #56303
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant