Repository navigation
Conversation
Adds AI_EXTRACT_RECORD(text, json_schema[, model[, endpoint]]), which returns a STRUCT derived from the JSON Schema's properties at bind time, mirroring DuckDB's ai_extract_record. Configuration is read from OPENAI_COMPATIBLE_API_KEY / _BASE_URL / _MODEL environment variables so credentials never appear in SQL. - schema -> STRUCT type mapping: integer/INT64, number/DOUBLE, boolean/BOOL, array/LIST(STRING), string/STRING - parallel per-row dispatch, same worker ceiling as AI_EXTRACT - NULL input yields NULL; unparseable model output yields a NULL struct - add ai_extract_record_error.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The existing
AI_EXTRACTreturns an untyped string and takes the API key, model and endpoint as SQL arguments. That works for ad-hoc queries, but not for two common deployments:triage.urgency_score >= 7, notjson_extractcalls in every downstream query.Like DuckDB's
ai_extract_record, this function derives the result type from a constant JSON Schema at bind time. Unlike DuckDB's version, provider configuration is read from environment variables so credentials stay out of SQL:OPENAI_COMPATIBLE_API_KEYOPENAI_COMPATIBLE_BASE_URLOPENAI_COMPATIBLE_MODELIf a model or endpoint argument is omitted or empty, the corresponding environment variable is used, falling back to
gpt-4o-miniandhttps://api.openai.com/v1. The variable names are the same ones DuckDB's AI functions use, so one deployment configuration works forboth engines.
Schema to type mapping
json_schemamust be a constant string whose top-level"type"is"object". Each property becomes a STRUCT field:integerINT64numberDOUBLEbooleanBOOLarrayLIST(STRING)string(and anything else)STRINGdescription,enum,minimum,requiredand similar annotations are forwarded to the model but do not affect the bound type. This is the same projection rule as DuckDB'sai_extract_record.Example
Support ticket triage, the same scenario used in the DuckDB AI cookbook:
Actual output (
model: glm-5.3-flashagainst an OpenAI-compatible gateway):Fields are ordinary typed columns, so results can be filtered and aggregated in Cypher without JSON parsing:
As with DuckDB, call the function once per row and project the fields in an outer
WITH/RETURN, so each row costs exactly one request.Behavior notes
AI_EXTRACTuses.NULLtext produces aNULLSTRUCT without contacting the provider; the API key is only required when at least one non-null row is sent.NULLfor that row instead of failing the statement (equivalent to DuckDB'son_error := 'null'). Markdown code fences around the JSON are stripped.<source_text>tags, and the system prompt instructs the model to treat it as data rather than instructions.STRING; nested STRUCT projection can be added later without changing the function signature.