Repository navigation
fix(datasets): render JSON-string metadata as expanded JSON in item editor (#14751) - #14908
wuisabel-gif wants to merge 1 commit into
Conversation
…ditor (langfuse#14751) JSON-string field values (common for OTLP-ingested traces, where non-primitive attributes get serialized to strings) showed as an escaped blob in the dataset-item editor. The trace viewer deep-parses and pretty-prints the same values, so the two views disagreed. Both editor stringify paths now run the value through the shared `deepParseJson` before formatting: `formatJsonValue` for the add-from-trace form and `stringifyDatasetItemData` for the edit dialog. Objects are cloned first because `deepParseJson` mutates in place and the value is shared React state. One behavior change: the editor now saves the deep-parsed form, so a metadata attribute stored as a JSON string gets persisted as an object on the next save. That matches what the issue asks for.
|
|
1 similar comment
|
|
| const parsed = deepParseJson( | ||
| typeof data === "object" ? structuredClone(data) : data, | ||
| ); |
There was a problem hiding this comment.
The
typeof data === "object" guard is true for null (standard JS quirk), so it would call structuredClone(null) when data is null. That call succeeds and returns null, so there's no runtime error here — null is already excluded by the !data early-return above — but the asymmetry with the parallel guard in NewDatasetItemForm.tsx (typeof value === "object" && value !== null) is a latent confusion risk if the early-return is ever adjusted.
| const parsed = deepParseJson( | |
| typeof data === "object" ? structuredClone(data) : data, | |
| ); | |
| const parsed = deepParseJson( | |
| typeof data === "object" && data !== null ? structuredClone(data) : data, | |
| ); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: web/src/features/datasets/utils/datasetItemUtils.ts
Line: 13-15
Comment:
The `typeof data === "object"` guard is `true` for `null` (standard JS quirk), so it would call `structuredClone(null)` when `data` is `null`. That call succeeds and returns `null`, so there's no runtime error here — `null` is already excluded by the `!data` early-return above — but the asymmetry with the parallel guard in `NewDatasetItemForm.tsx` (`typeof value === "object" && value !== null`) is a latent confusion risk if the early-return is ever adjusted.
```suggestion
const parsed = deepParseJson(
typeof data === "object" && data !== null ? structuredClone(data) : data,
);
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| @@ -9,7 +9,11 @@ export const stringifyDatasetItemData = (data: unknown): string => { | |||
| if (!data) return ""; | |||
There was a problem hiding this comment.
Falsy-value short-circuit silently swallows valid data — The
!data guard returns "" for any falsy value: 0, false, and "" are all valid Prisma.JsonValue entries that would be silently dropped instead of serialized. This is pre-existing behavior, but the new deepParseJson path now relies on this guard for null protection in structuredClone, making the coupling more load-bearing than it was before.
Prompt To Fix With AI
This is a comment left during a code review.
Path: web/src/features/datasets/utils/datasetItemUtils.ts
Line: 9
Comment:
**Falsy-value short-circuit silently swallows valid data** — The `!data` guard returns `""` for any falsy value: `0`, `false`, and `""` are all valid `Prisma.JsonValue` entries that would be silently dropped instead of serialized. This is pre-existing behavior, but the new `deepParseJson` path now relies on this guard for null protection in `structuredClone`, making the coupling more load-bearing than it was before.
How can I resolve this? If you propose a fix, please make it concise.
What
The dataset-item editor showed JSON-string metadata as an escaped blob, like
"{\"tools\":[...]}". The trace viewer showed the same value as expanded, readable JSON. Values like this are common on OTLP-ingested traces, where non-primitive attributes get serialized to strings. In the editor the escaping made the value hard to read and near impossible to edit by hand.Fixes #14751.
Why it happened
Two paths build the string the editor shows, and both called
JSON.stringifydirectly:formatJsonValueinNewDatasetItemForm(the "Add to dataset" form)stringifyDatasetItemDataindatasetItemUtils(the edit dialog)Neither unwrapped nested JSON strings, so a stringified value stayed escaped. The trace viewer already handles this by running values through
deepParseJson.The fix
Both paths now run the value through the shared
deepParseJsonbefore formatting, so the editor matches the trace viewer. Objects are cloned first, becausedeepParseJsonmutates its input in place and the value comes from shared React state.Behavior change to flag
The editor now saves the deep-parsed form. A metadata attribute stored as a JSON string gets written back as a real object the next time the item is saved. That is what the issue asks for, but a maintainer should confirm it is the wanted behavior.
Testing
Added
datasetItemUtils.clienttest.tscovering nested JSON strings, top-level JSON strings, plain strings, null, and the no-mutation guarantee. I could not run the full suite locally because of an unrelatedpnpm installproblem on my machine, so please let CI confirm.Greptile Summary
This PR fixes the dataset item editor rendering JSON-string metadata as an escaped blob by routing both
formatJsonValue(inNewDatasetItemForm) andstringifyDatasetItemData(indatasetItemUtils) throughdeepParseJsonbefore formatting, matching what the trace viewer already does. A small test suite is added covering the key scenarios.deepParseJsonis now applied (with astructuredCloneguard) beforeJSON.stringifyin both display paths, so OTLP-ingested metadata attributes stored as JSON strings are shown as readable, expanded JSON rather than escaped blobs.Confidence Score: 3/5
The display fix is solid, but
stringifyDatasetItemDatasilently drops valid falsyPrisma.JsonValueinputs (0,false,"") via the!dataguard, and on save those values would be written back as deep-parsed objects — a combination that warrants a second look before merging.The
!dataearly-return instringifyDatasetItemDatawas pre-existing but is now load-bearing for thestructuredClonenull-protection path. Any falsyPrisma.JsonValue—0,false, or empty string — gets silently serialized as""instead of the correct JSON representation, and if the user saves after viewing, that field would either be dropped or overwritten. The fix itself (deep-parsing nested strings) is correct and well-tested, andNewDatasetItemForm.tsxhandles the same cases cleanly.web/src/features/datasets/utils/datasetItemUtils.ts — the
!dataguard and the missingnullcheck onstructuredCloneFlowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Dataset Item Editor opens] --> B{Field value type?} B -->|Prisma.JsonValue - object/array| C[structuredClone value] B -->|Prisma.JsonValue - string / primitive| D[Use value as-is] C --> E[deepParseJson] D --> E E --> F{Contains nested JSON strings?} F -->|Yes - e.g. OTLP trace metadata| G[Recursively parse string values into objects] F -->|No| H[Return value unchanged] G --> I[JSON.stringify with 2-space indent] H --> I I --> J[Display in CodeMirror editor] J --> K{User saves item?} K -->|Yes| L[Saves deep-parsed form\nJSON strings become real objects] K -->|No| M[No data mutation]%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% flowchart TD A[Dataset Item Editor opens] --> B{Field value type?} B -->|Prisma.JsonValue - object/array| C[structuredClone value] B -->|Prisma.JsonValue - string / primitive| D[Use value as-is] C --> E[deepParseJson] D --> E E --> F{Contains nested JSON strings?} F -->|Yes - e.g. OTLP trace metadata| G[Recursively parse string values into objects] F -->|No| H[Return value unchanged] G --> I[JSON.stringify with 2-space indent] H --> I I --> J[Display in CodeMirror editor] J --> K{User saves item?} K -->|Yes| L[Saves deep-parsed form\nJSON strings become real objects] K -->|No| M[No data mutation]Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(datasets): render JSON-string metada..." | Re-trigger Greptile