Skip to content

Commit 7ff070e

Browse files
committed
fix(json): preserve fenced structured replies
1 parent fada7aa commit 7ff070e

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

src/json.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export function isParseableJson(text: string): boolean {
8484
export function mergePrefill(prefill: string, raw: string): string {
8585
const trimmed = raw.trimStart()
8686
const trimmedPrefill = prefill.trimEnd()
87+
if (extractJsonFence(trimmed) !== undefined) {
88+
return raw
89+
}
8790
if (trimmed.startsWith(trimmedPrefill)) {
8891
return raw
8992
}

test/json.test.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ describe('json', () => {
4646
expect(mergePrefill('{"a":', '{"a":1}')).toBe('{"a":1}')
4747
})
4848

49+
it('does not prepend a prefill to a complete fenced response', () => {
50+
const raw = '```json\n{"a":1}\n```'
51+
expect(mergePrefill('{"a":', raw)).toBe(raw)
52+
})
53+
4954
it('wraps an array-element continuation of a nested-array prefill', () => {
5055
// The model continued from `{"updates":[` without echoing it, so raw alone
5156
// is unbalanced (`{…}]}`) but prefill+raw parses.

0 commit comments

Comments
 (0)