Repository navigation
Fixes #4930: cap Titan Bedrock embedding input to avoid ValidationException (#29920) - #30021
Conversation
…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>
❌ PR checklist incompleteThis PR cannot be merged until the following are addressed on its linked issue:
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 |
| static String cap(String text) { | ||
| return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS); | ||
| } |
There was a problem hiding this comment.
💡 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 👍 / 👎
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsBackports 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
🤖 Prompt for agentsOptionsDisplay: compact → Showing less information. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
| static String cap(String text) { | ||
| return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS); | ||
| } |
There was a problem hiding this comment.
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.
| 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)
| static String cap(String text) { | ||
| return StringUtils.truncate(text, TITAN_MAX_INPUT_CHARS); | ||
| } |
There was a problem hiding this comment.
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.
| 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)
|
The Python checkstyle failed. Please run You can install the pre-commit hooks with |
|
🔴 Playwright Results — 1 failure(s), 16 flaky✅ 3961 passed · ❌ 1 failed · 🟡 16 flaky · ⏭️ 30 skipped
Genuine Failures (failed on all attempts)❌
|



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
StringUtils.truncate) before it reaches Titan'sinputText.ValidationException, halve the already-capped input and retry (up to 3 times). Detection is a typedValidationExceptioncheck plus an"input token"message marker, so throttling/auth/other validation errors are never retried.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'sBedrockEmbeddingClienthas aBedrockEmbeddingFamilyenum (Titan V1/V2 + Cohere), adoEmbedQuerypath, and per-family request builders.1.13's client is Titan-only with a single inlinedoEmbed.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:
Note (pre-existing, not addressed here)
1.13always sendsdimensionsin the Titan payload, whichamazon.titan-embed-text-v1does not accept. That's a pre-existing1.13bug unrelated to #4930 — the family refactor is what fixed it onmain— 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:
inputText.ValidationExceptions with halved input.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.
openmetadata-service/src/main/java/org/openmetadata/service/search/vector/client/BedrockEmbeddingClient.java
Important Files Changed
Reviews (1): Last reviewed commit: "Fixes #4930: cap Titan Bedrock embedding..." | Re-trigger Greptile
Context used: