Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: workaround buggy traces that specify parser: json but have non-json value #699

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pdl-live-react/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ function tryJson(s: unknown) {
return s
}

export function tryJsonPrettyPrint(s: string) {
try {
return JSON.stringify(JSON.parse(s), undefined, 2)
} catch (_err) {
// intentional fall-through
}
return s
}

export function extractStructuredModelResponse({
pdl__result,
parser,
Expand Down
3 changes: 2 additions & 1 deletion pdl-live-react/src/view/code/Code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { lazy, Suspense } from "react"
import { stringify } from "yaml"
import { match, P } from "ts-pattern"

import { tryJsonPrettyPrint } from "../../helpers"
import { type PdlBlock } from "../../pdl_ast"
import { map_block_children } from "../../pdl_ast_utils"

Expand Down Expand Up @@ -34,7 +35,7 @@ export default function Code({
const value =
typeof block === "string"
? language === "json"
? JSON.stringify(JSON.parse(block), undefined, 2)
? tryJsonPrettyPrint(block)
: block
: stringify(raw ? block : block_code_cleanup(block))

Expand Down