MCPcopy Create free account
hub / github.com/TanStack/ai / PartialJSONParser

Class PartialJSONParser

packages/ai/src/activities/chat/stream/json-parser.ts:25–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23 * This parser can handle incomplete JSON strings during streaming
24 */
25export class PartialJSONParser implements JSONParser {
26 /**
27 * Parse a potentially incomplete JSON string
28 * @param jsonString - The JSON string to parse (may be incomplete)
29 * @returns The parsed object, or undefined if parsing fails
30 */
31 parse(jsonString: string): any {
32 if (!jsonString || jsonString.trim() === '') {
33 return undefined
34 }
35
36 try {
37 return parsePartialJSONLib(jsonString)
38 } catch {
39 // If partial parsing fails, return undefined
40 // This is expected during early streaming when we have very little data
41 return undefined
42 }
43 }
44}
45
46/**
47 * Default parser instance

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected