qanary-component-helpers/generate-object-retry
This page is auto-generated from TSDoc output. Edit the source file, not this Markdown.
qanary-component-helpers/generate-object-retry
Section titled “qanary-component-helpers/generate-object-retry”- Package:
@leipzigtreechat/qanary-component-helpers - Source file:
packages/qanary-component-helpers/src/generate-object-retry.ts
Summary
Section titled “Summary”Extracts the first valid JSON object from text that may be wrapped in markdown code fences, preceded by prose, or followed by explanations.
Extraction strategy (in order):
- Content inside a markdown code fence (json … or … )
- First well-formed JSON object
{…}found anywhere in the text
Functions
Section titled “Functions”extractJsonFromText
Section titled “extractJsonFromText”function extractJsonFromText(text: string): string | nullExtracts the first valid JSON object from text that may be wrapped in markdown code fences, preceded by prose, or followed by explanations.
Extraction strategy (in order):
- Content inside a markdown code fence (json … or … )
- First well-formed JSON object
{…}found anywhere in the text
Parameters
text(string) Returnsstring | null: The extracted JSON string (trimmed), ornullif nothing parseable was found. Examples extractJsonFromText(‘Here:njsonn”a”:1nnDone.’) // ‘“a”:1’ extractJsonFromText(‘“b”:2’) // ‘“b”:2’ extractJsonFromText(‘No JSON here.’) // null Defined at: line 20
getRawTextFromError
Section titled “getRawTextFromError”function getRawTextFromError(error: unknown): string | undefinedRetrieves the raw LLM response text stored on AI SDK error objects.
Both NoObjectGeneratedError and JSONParseError (from @ai-sdk/provider)
expose the original model response via a text property on the thrown
error instance.
Parameters
error(unknown) Returnsstring | undefinedDefined at: line 95
generateObjectWithRetry
Section titled “generateObjectWithRetry”function generateObjectWithRetry(options: { model: LanguageModel; /** * A Zod (v3 or v4) schema – or any object with a compatible `safeParse` * method – that describes the expected response shape. */ schema: ParseableSchema<T>; system?: string; prompt: string; }, maxRetries: unknown): Promise<{ object: T }>Drop-in replacement for the AI SDK’s generateObject that adds two
recovery strategies for models that do not return clean JSON:
Strategy 1 – Markdown extraction (no extra LLM call):
Some instruction-tuned models (e.g. anthropic/claude-3.5-haiku via
OpenRouter) wrap their JSON response in a markdown code fence even when
asked not to. The AI SDK then throws a parse error that carries the full
raw response in its text property. We extract the JSON from that text
and validate it against the caller’s schema before spending another token.
Strategy 2 – Retry with explicit JSON reminder: If extraction fails (or the error had no raw text), we retry the full LLM call. On every retry we append a reminder to the prompt asking for plain JSON without markdown, giving the model a chance to self-correct.
Parameters
options({ model: LanguageModel; /**- A Zod (v3 or v4) schema – or any object with a compatible
safeParse - method – that describes the expected response shape.
*/
schema: ParseableSchema
; system?: string; prompt: string; }): Same options accepted by the AI SDK’s generateObject(model,schema,system,prompt).
- A Zod (v3 or v4) schema – or any object with a compatible
maxRetries(unknown): Maximum number of LLM calls in total. Defaults to3. ReturnsPromise<{ object: T }>:{ object: T }– same shape asgenerateObject. Defined at: line 137
ParseableSchema
Section titled “ParseableSchema”type ParseableSchema = { safeParse(data: unknown): { success: boolean; data?: T };}Minimal schema interface required by .
Structurally compatible with both Zod v3 and Zod v4 schemas – no direct Zod import is needed in this package.
Defined at: line 111
In qanary-component-helpers
Section titled “In qanary-component-helpers”qanary-component-helpers/apiqanary-component-helpers/baseqanary-component-helpers/commonqanary-component-helpers/configurationqanary-component-helpers/create-annotationqanary-component-helpers/create-clarification-annotationqanary-component-helpers/generate-clarification-questionqanary-component-helpers/generate-object-retryqanary-component-helpers/get-domain-instancesqanary-component-helpers/get-question-uriqanary-component-helpers/get-questionqanary-component-helpers/indexqanary-component-helpers/interfaces/question-sparql-responseqanary-component-helpers/llm-providerqanary-component-helpers/message-operationsqanary-component-helpers/query-file-loaderqanary-component-helpers/query-sparqlqanary-component-helpers/utils/question-uri-query