Skip to content
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ result = parse("-Inf");
console.log(result); // Outputs: -Infinity
```

### Parsing model replies with Markdown fences

`parse` expects the JSON text itself. If a model wraps JSON in Markdown fences or surrounding prose, strip that wrapper before passing the string to `parse`:

````js
const reply = "Here is the JSON:\n```json\n{\"status\": \"ok\"}\n```";
const match = reply.match(/```(?:json)?\s*([\s\S]*?)\s*```/i);
const json = match ? match[1] : reply;

parse(json); // Outputs: { status: "ok" }
````

### Handling malformed JSON

If the JSON string is malformed, the `parse` function will throw an error:
Expand Down