Repository navigation
Fixes #32600: render table sample rows in the AI context markdown - #33126
Conversation
#32608 added `assetContext.table.sampleData` to the AI context but only the structured representation surfaced it. Markdown is the default for both `GET /{entity}/{id}/context` and the MCP `get_entity` tool, so the stored samples were reachable only via `?format=json` — the gap this closes. Renders a `# Sample Data` table under the SCHEMA section, immediately reusing the payload the builder already authorized against `ViewSampleData` and PII-masked. Values are flattened to one line, pipe escaped, padded/truncated to the declared column width, and capped at 120 chars so a blob column cannot crowd out the rest of the context. A caveat line marks the rows as a sample so an agent does not count or aggregate over them. The persona context path is unaffected: `AIContextBuilder.applySearchFields` indexes `buildTableContext(table)` without sample data, so the cached, cross-user persona document has nothing to render. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
| private static String sampleCell(Object value) { | ||
| String text = value == null ? "" : cell(Objects.toString(value).replaceAll("\\R+", " ")); | ||
| return text.length() > MAX_SAMPLE_CELL_CHARS | ||
| ? text.substring(0, MAX_SAMPLE_CELL_CHARS) + "…" | ||
| : text; | ||
| } |
There was a problem hiding this comment.
💡 Edge Case: Sample cell truncation can split a char or escape sequence
In sampleCell, text.substring(0, MAX_SAMPLE_CELL_CHARS) cuts the already-escaped string at a UTF-16 code-unit boundary. This can split a surrogate pair (leaving a lone surrogate) or split an inserted \| escape, leaving a trailing backslash before the appended … (rendered as a visible \…). The impact is cosmetic — it does not break table alignment since no new | is introduced — but truncating on code points and trimming a dangling backslash would be cleaner. Consider capping on codePointCount/offsetByCodePoints and stripping a trailing lone \ before appending the ellipsis.
Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsAdds Consider refining sample cell truncation to avoid splitting UTF-16 surrogates or escape sequences—currently 💡 Edge Case: Sample cell truncation can split a char or escape sequence📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextMarkdown.java:488-493 In 🤖 Prompt for agentsOptionsDisplay: compact → Counting what did not apply, without listing it. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source |
Describe your changes:
Fixes #32600
I reopened #32600 because the change that closed it (#32608) only surfaced the samples in the
structured representation. Markdown is what both consumers return by default —
GET /v1/tables/{id}/contextwith noformatparam, and the MCPget_entitytool withinclude: ["context"]— soassetContext.table.sampleDatawas populated but never rendered, andthe stored rows stayed unreachable for exactly the SQL-agent path the issue was filed for. That PR's
own description states it: "The default Markdown rendering is unchanged."
This renders a
# Sample Datasection into the markdown document from the payload the builderalready authorized against
ViewSampleDataand PII-masked, so no new data reaches a caller whocould not already read it via
?format=json.Type of change:
High-level design:
AIContextMarkdown.appendTableContextgainsappendSampleData, placed under the existingSCHEMAsection gate after the schema table and data model — definitions first, then values.values are flattened to one line (
\R+→ space), pipes escaped,nullrendered as an emptycell, rows padded or truncated to the declared column width, and each cell capped at 120 chars.
The width normalization matters for correctness, not just looks — one ragged row otherwise shifts
every value under the wrong column name, which is worse than no samples at all for an agent.
_Representative stored sample rows — not the full table; never count or aggregate over them._) precedes the table, so an agent does not read 10 rows as the table's cardinality.ContextSectionenum value was added, deliberately.AIContextBuilder.applySearchFieldsindexesbuildTableContext(table)without sample data, andPersonaContextBuilder.assetContext(document)deserializesTableContextfrom that searchdocument. So the persona path has no samples to render, and a
sampleDatapersona toggle wouldadvertise a section that path can never fill. Materializing samples there would also be a real
leak: the persona context document is cached and shared across users, so it cannot carry a
per-caller
ViewSampleData/PII decision. Gating onSCHEMAkeeps the section on the one paththat does the per-request authorization.
sampleDataalready exists onassetContext.tablefromFixes #32600: include samples in AI context #32608. This PR is presentation-only.
Tests:
Use cases covered
?format=json) receives a# Sample Datatable with the header row and the first 10 of 12 stored rows.deny ViewSampleDatareceives the markdown context with# Schemaandno
# Sample Datasection and no sample values anywhere in the body.short rows padded, overflowing values dropped rather than appended as phantom cells.
nulldoes not break the table delimiters.Unit tests
openmetadata-service/src/test/java/org/openmetadata/service/aicontext/AIContextMarkdownTest.java(5 new tests:
render_emitsSampleDataRowsUnderSchema,render_omitsSampleDataWhenAbsentOrEmpty,render_sanitizesAndPadsDelimiterSensitiveSampleValues,render_padsRaggedSampleRowsToTheColumnWidth,render_capsLongSampleValues)mvn -Pstatic-code-analysis ... jacoco:report):AIContextMarkdown.java— 90.8% lines (376/414), 79.7% branches. Meets the 90% target.Backend integration tests
openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/mcp/AIContextRestIT.javatableContext_returnsOkfMarkdownByFqnnow asserts the# Sample Dataheading, the header row,row 9, and the absence of
name-10(the 10-row cap holds in markdown too).tableContext_omitsSamplesFromMarkdownWithoutViewSampleDataPermission(new) — the deniedcaller's markdown has
# Schemabut no# Sample Dataand no sample values.Ingestion integration tests
Playwright (UI) tests
Manual testing performed
null,a 200-char blob, and a 3-column header against 1- and 4-value rows; confirmed the table stays
aligned,
a|brenders asa\|b, the newline flattens, the blob elides with…, and the caveatline precedes the table:
mvn -o -pl openmetadata-service test -Dtest=AIContextMarkdownTest,AIContextBuilderTest— pass.mvn -o -pl openmetadata-service test -Dtest=PersonaContextMarkdownTest,PersonaContextBuilderTest— pass, confirming the persona rendering path is unchanged.
mvn -o -pl openmetadata-integration-tests test-compile— clean.mvn -pl openmetadata-service,openmetadata-integration-tests spotless:apply— no diff.UI screen recording / screenshots:
Not applicable.
Checklist:
Fixes <issue-number>: <short explanation>Fixes #<issue-number>above.sampleDataalready exists onassetContext.tablefrom Fixes #32600: include samples in AI context #32608.Bug fix
origin/main'sAIContextMarkdown.javaagainst the new test file givesTests run: 29, Failures: 4— the 4 assertion tests fail without the fix. The 5th(
render_omitsSampleDataWhenAbsentOrEmpty) asserts absence and so passes onmainbyconstruction; it guards against a future regression that renders an empty section, not
against today's bug.