Skip to content

fix(datasets): render JSON-string metadata as expanded JSON in item editor (#14751) - #14908

Open
wuisabel-gif wants to merge 1 commit into
langfuse:mainfrom
wuisabel-gif:fix/dataset-metadata-json-editor
Open

wuisabel-gif wants to merge 1 commit into
langfuse:mainfrom
wuisabel-gif:fix/dataset-metadata-json-editor

Conversation

@wuisabel-gif

@wuisabel-gif wuisabel-gif commented Jul 8, 2026 •

Copy link
Copy Markdown

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.stringify directly:

  • formatJsonValue in NewDatasetItemForm (the "Add to dataset" form)
  • stringifyDatasetItemData in datasetItemUtils (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 deepParseJson before formatting, so the editor matches the trace viewer. Objects are cloned first, because deepParseJson mutates 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.ts covering 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 unrelated pnpm install problem 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 (in NewDatasetItemForm) and stringifyDatasetItemData (in datasetItemUtils) through deepParseJson before formatting, matching what the trace viewer already does. A small test suite is added covering the key scenarios.

  • deepParseJson is now applied (with a structuredClone guard) before JSON.stringify in both display paths, so OTLP-ingested metadata attributes stored as JSON strings are shown as readable, expanded JSON rather than escaped blobs.
  • The change carries an acknowledged behavioral side-effect: saving a dataset item after viewing it will persist the deep-parsed (object) form of any previously-stringified metadata field, converting stored JSON strings to real objects.

Confidence Score: 3/5

The display fix is solid, but stringifyDatasetItemData silently drops valid falsy Prisma.JsonValue inputs (0, false, "") via the !data guard, and on save those values would be written back as deep-parsed objects — a combination that warrants a second look before merging.

The !data early-return in stringifyDatasetItemData was pre-existing but is now load-bearing for the structuredClone null-protection path. Any falsy Prisma.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, and NewDatasetItemForm.tsx handles the same cases cleanly.

web/src/features/datasets/utils/datasetItemUtils.ts — the !data guard and the missing null check on structuredClone

Flowchart

%%{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]
Loading
%%{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]
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
web/src/features/datasets/utils/datasetItemUtils.ts:13-15
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,
    );
```

### Issue 2 of 2
web/src/features/datasets/utils/datasetItemUtils.ts:9
**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.

Reviews (1): Last reviewed commit: "fix(datasets): render JSON-string metada..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

…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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@wuisabel-gif
wuisabel-gif marked this pull request as ready for review July 8, 2026 21:38

@claude claude Bot left a comment

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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. feat-datasets labels Jul 8, 2026
Comment on lines +13 to +15
const parsed = deepParseJson(
typeof data === "object" ? structuredClone(data) : data,
);

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 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.

Suggested change
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 "";

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 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat-datasets size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

2 participants