Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: adding a few more tests after #13644 (#13967)

Valery Meleshkin4f99e03cca2c
diff --git a/packages/shared/src/server/queries/clickhouse-sql/search.ts b/packages/shared/src/server/queries/clickhouse-sql/search.ts
index 52c7b1ac04..7248b3a034 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/search.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/search.ts
@@ -10,7 +10,7 @@ const regexIndefiniteCharacters = "%";
* UTF-16 surrogate pair). ASCII is left untouched.
*
* Trace / observation `input` and `output` ingested through that path is persisted in ClickHouse
- * verbatim in this escaped form (e.g. `你好` is stored as the literal `你好`), so a
+ * verbatim in this escaped form (e.g. `你好` is stored as the literal `\u4f60\u597d`), so a
* substring search for the raw, human-readable text would never match it. By also searching for
* this escaped form we recover full-text search for non-English content. See issue #11538.
*/
diff --git a/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts b/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts
index 3bc39cd024..fcfa9a081f 100644
--- a/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts
+++ b/web/src/__tests__/server/clickhouseSearchCondition.servertest.ts
@@ -2,8 +2,12 @@ import { env } from "@/src/env.mjs";
import { type TracingSearchType } from "@langfuse/shared";
import {
clickhouseSearchCondition,
+ createEvent,
+ createEventsCh,
+ getObservationsWithModelDataFromEventsTable,
queryClickhouse,
} from "@langfuse/shared/src/server";
+import { randomUUID } from "crypto";
const maybeEventsTable =
env.LANGFUSE_ENABLE_EVENTS_TABLE_V2_APIS === "true"
@@ -25,6 +29,8 @@ const searchFixture = `
('output-match-cyrillic', 'plain name', 'unrelated input', 'contains привет token'),
('input-match-cjk', 'plain name', 'contains 東京 token', 'unrelated output'),
('output-match-cjk', 'plain name', 'unrelated input', 'contains 東京 token'),
+ ('input-match-escaped-cjk', 'plain name', 'contains \\\\u6771\\\\u4eac token', 'unrelated output'),
+ ('output-match-escaped-cjk', 'plain name', 'unrelated input', 'contains \\\\u6771\\\\u4eac token'),
('miss', 'plain name', 'unrelated input', 'unrelated output')
) AS e
`;
@@ -123,10 +129,15 @@ describe("clickhouseSearchCondition", () => {
{
query: "東京",
searchType: ["content"],
- expectedIds: ["input-match-cjk", "output-match-cjk"],
+ expectedIds: [
+ "input-match-cjk",
+ "input-match-escaped-cjk",
+ "output-match-cjk",
+ "output-match-escaped-cjk",
+ ],
},
])(
- "preserves baseline substring search semantics for $query $searchType search",
+ "matches expected substring rows for $query $searchType search",
async ({ query, searchType, expectedIds }) => {
await expect(
matchingIds({
@@ -151,6 +162,116 @@ describe("clickhouseSearchCondition", () => {
expect(baseIds).toEqual(["id-match"]);
expect(ftsIds).toEqual(baseIds);
});
+
+ it("matches JSON-escaped Unicode content on events tables with the FTS prefilter", async () => {
+ const search = clickhouseSearchCondition({
+ query: "東京",
+ searchType: ["content"],
+ tablePrefix: "e",
+ searchColumns: ["id", "name"],
+ useEventsTablePath: true,
+ });
+
+ expect(search.params).toMatchObject({
+ searchString: "%東京%",
+ searchStringEscaped: "%\\u6771\\u4eac%",
+ });
+ expect(search.query).toContain("{searchStringEscaped: String}");
+ expect(search.query).toContain(
+ "hasAllTokens(lower(e.input), lower({searchStringEscaped: String}))",
+ );
+ expect(search.query).toContain(
+ "hasAllTokens(lower(e.output), lower({searchStringEscaped: String}))",
+ );
+
+ await expect(
+ matchingIds({
+ query: "東京",
+ searchType: ["content"],
+ useEventsTablePath: true,
+ }),
+ ).resolves.toEqual([
+ "input-match-cjk",
+ "input-match-escaped-cjk",
+ "output-match-cjk",
+ "output-match-escaped-cjk",
+ ]);
+ });
+
+ it("round-trips raw and JSON-escaped Unicode through events_full search", async () => {
+ const uniqueProjectId = randomUUID();
+ const traceId = randomUUID();
+ const rawSpanId = randomUUID();
+ const escapedSpanId = randomUUID();
+ const rawInput = `{"message":"東京"}`;
+ const escapedInput = `{"message":"\\u6771\\u4eac"}`;
+
+ await createEventsCh([
+ createEvent({
+ id: rawSpanId,
+ span_id: rawSpanId,
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ type: "GENERATION",
+ name: "raw-unicode-search-roundtrip",
+ input: rawInput,
+ output: "ascii output",
+ }),
+ createEvent({
+ id: escapedSpanId,
+ span_id: escapedSpanId,
+ project_id: uniqueProjectId,
+ trace_id: traceId,
+ type: "GENERATION",
+ name: "escaped-unicode-search-roundtrip",
+ input: escapedInput,
+ output: "ascii output",
+ }),
+ ]);
+
+ const stored = await queryClickhouse<{ span_id: string; input: string }>({
+ query: `
+ SELECT span_id, input
+ FROM events_full
+ WHERE project_id = {projectId: String}
+ AND span_id IN ({rawSpanId: String}, {escapedSpanId: String})
+ ORDER BY span_id ASC
+ `,
+ params: {
+ projectId: uniqueProjectId,
+ rawSpanId,
+ escapedSpanId,
+ },
+ preferredClickhouseService: "EventsReadOnly",
+ tags: {
+ feature: "clickhouse-search-condition-test",
+ type: "events",
+ kind: "test",
+ },
+ });
+
+ expect(stored).toHaveLength(2);
+ expect(rawInput).not.toBe(escapedInput);
+
+ const storedInputBySpanId = new Map(
+ stored.map((row) => [row.span_id, row.input]),
+ );
+ expect(storedInputBySpanId.get(rawSpanId)).toBe(rawInput);
+ expect(storedInputBySpanId.get(escapedSpanId)).toBe(escapedInput);
+
+ const observations = await getObservationsWithModelDataFromEventsTable({
+ projectId: uniqueProjectId,
+ filter: [],
+ searchQuery: "東京",
+ searchType: ["content"],
+ limit: 100,
+ offset: 0,
+ });
+
+ expect(observations.map((observation) => observation.id).sort()).toEqual(
+ [rawSpanId, escapedSpanId].sort(),
+ );
+ });
});
it("generates FTS predicates for events input/output searches", () => {
diff --git a/web/src/__tests__/server/multilingual-fulltext-search.servertest.ts b/web/src/__tests__/server/multilingual-fulltext-search.servertest.ts
index 547cd4af28..c837f32315 100644
--- a/web/src/__tests__/server/multilingual-fulltext-search.servertest.ts
+++ b/web/src/__tests__/server/multilingual-fulltext-search.servertest.ts
@@ -7,9 +7,9 @@
* Langfuse Python SDK v3 uses, and what the bug reporter used), its `input` / `output`
* payload is stored in ClickHouse **verbatim as the JSON string the SDK produced**. Python's
* `json.dumps(...)` defaults to `ensure_ascii=True`, so a value like `你好` is persisted on
- * disk as the literal 12-byte ASCII string `你好`. The UI's display layer parses that
+ * disk as the literal 12-byte ASCII string `\u4f60\u597d`. The UI's display layer parses that
* JSON back, so the user *sees* `你好` — but full-text search (`clickhouseSearchCondition`)
- * runs a literal `input ILIKE '%你好%'`, which can never match the stored bytes `你好`.
+ * runs a literal `input ILIKE '%你好%'`, which can never match the stored bytes `\u4f60\u597d`.
* ASCII text (`Hello`) is not escaped, so it matches.
*
* These tests reproduce that: they store trace/observation I/O in the exact escaped form the
@@ -336,7 +336,7 @@ describe("multilingual full-text search (issue #11538)", () => {
name: `ml-emoji-${randomUUID()}`,
input: { status: "Deployment 🚀 succeeded" },
});
- // 🚀 (U+1F680) is persisted as the literal sequence 🚀; a correct fix encodes the
+ // 🚀 (U+1F680) is persisted as the literal sequence \ud83d\ude80; a correct fix encodes the
// query the same way.
expect(await searchTraceIds("🚀", ["id", "content"])).toContain(trace.id);
});
@@ -344,7 +344,7 @@ describe("multilingual full-text search (issue #11538)", () => {
it("finds a trace by a supplementary-plane CJK ideograph (CJK Ext. B)", async () => {
const trace = await insertEscapedTrace({
name: `ml-cjk-ext-b-${randomUUID()}`,
- // 𠀀 = U+20000, 𠀁 = U+20001 — both stored as surrogate pairs (𠀀, 𠀁).
+ // 𠀀 = U+20000, 𠀁 = U+20001 — both stored as surrogate pairs (\ud840\udc00, \ud840\udc01).
input: { rareChars: "古文字 𠀀𠀁 测试" },
});
expect(await searchTraceIds("𠀀𠀁", ["id", "content"])).toContain(