Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

feat(exa): expose grounded structured search output

Rudy Celeklibc35fc1fd9fb
diff --git a/tests/clis/exa-structured-search.test.cjs b/tests/clis/exa-structured-search.test.cjs
new file mode 100644
index 0000000..c726a65
--- /dev/null
+++ b/tests/clis/exa-structured-search.test.cjs
@@ -0,0 +1,26 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/exa.js')
+function run(args, network = true) {
+ const code=`global.fetch=async(url,opts)=>{if(!${network})throw Error('Unexpected request');return{status:200,text:async()=>JSON.stringify({url,body:JSON.parse(opts.body),output:{content:{vendors:['Example']},grounding:[{field:'vendors',citations:[{url:'https://example.com'}],confidence:'low'}]},requestId:'test',costDollars:{total:0.02}})}};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath,['-e',code],{encoding:'utf8',timeout:5000,env:{...process.env,EXA_API_KEY:'fake-key'}})
+}
+function output(r){assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout)}
+const schema={type:'object',properties:{vendors:{type:'array',items:{type:'string'}}},required:['vendors']}
+test('structured search forwards the schema and research context and preserves grounding',()=>{
+ const data=output(run(['search','--query','newsletter platforms','--type','deep','--output-schema',JSON.stringify(schema),'--objective','Find vendors for a small publisher','--system-prompt','Use primary sources','--additional-queries',JSON.stringify(['newsletter pricing','newsletter deliverability']),'--highlights']));
+ assert.deepEqual(data.body.outputSchema,schema);assert.equal(data.body.objective,'Find vendors for a small publisher');assert.equal(data.body.systemPrompt,'Use primary sources');assert.deepEqual(data.body.additionalQueries,['newsletter pricing','newsletter deliverability']);assert.deepEqual(data.body.contents,{highlights:true});assert.equal(data.output.grounding[0].citations[0].url,'https://example.com');assert.equal(data.costDollars.total,0.02);
+})
+test('text schema and existing search filters work together in a redacted dry run',()=>{
+ const data=output(run(['search','--query','newsletters','--output-schema','{"type":"text"}','--include-domains','example.com','--num','3','--dry-run'],false));
+ assert.deepEqual(data.body.outputSchema,{type:'text'});assert.deepEqual(data.body.includeDomains,['example.com']);assert.equal(data.body.numResults,3);assert.equal(data.headers['x-api-key'],'***');assert.doesNotMatch(JSON.stringify(data),/fake-key/)
+})
+test('malformed schema and research parameters fail before a request',()=>{
+ const cases=[['--output-schema','not-json'],['--output-schema','[]'],['--output-schema','{"type":"array"}'],['--output-schema'],['--objective'],['--objective','x'.repeat(4097)],['--system-prompt'],['--additional-queries','[]'],['--additional-queries','[1]'],['--additional-queries','["q"]'],['--additional-queries',JSON.stringify(Array(11).fill('q')),'--type','deep']];
+ for(const args of cases){const r=run(['search','--query','q',...args],false);assert.equal(r.status,1,r.stdout);assert.match(r.stderr,/output-schema|objective|system-prompt|additional-queries/);assert.doesNotMatch(r.stderr,/Unexpected request/)}
+})
+test('plain search preserves its existing payload when structured options are omitted',()=>{
+ const data=output(run(['search','--query','newsletter platforms','--num','5']));assert.deepEqual(data.body,{query:'newsletter platforms',numResults:5});assert.equal(data.url,'https://api.exa.ai/search')
+})
diff --git a/tools/clis/exa.js b/tools/clis/exa.js
index 87b7629..d6d1f6f 100755
--- a/tools/clis/exa.js
+++ b/tools/clis/exa.js
@@ -70,6 +70,38 @@ function buildContents(args) {
return Object.keys(contents).length ? contents : null
}
+function buildResearch(args) {
+ const research = {}
+ if (args['output-schema'] !== undefined) {
+ let schema
+ try { schema = JSON.parse(args['output-schema']) } catch { throw new Error('--output-schema requires valid JSON') }
+ if (!schema || typeof schema !== 'object' || Array.isArray(schema) || !['text', 'object'].includes(schema.type)) {
+ throw new Error('--output-schema requires an object with root type text or object')
+ }
+ research.outputSchema = schema
+ }
+ for (const [flag, field] of [['objective', 'objective'], ['system-prompt', 'systemPrompt']]) {
+ if (args[flag] !== undefined) {
+ if (typeof args[flag] !== 'string' || !args[flag].trim() || (flag === 'objective' && args[flag].length > 4096)) {
+ throw new Error(`--${flag} requires nonempty text${flag === 'objective' ? ' of at most 4096 characters' : ''}`)
+ }
+ research[field] = args[flag]
+ }
+ }
+ if (args['additional-queries'] !== undefined) {
+ let queries
+ try { queries = JSON.parse(args['additional-queries']) } catch { throw new Error('--additional-queries requires a JSON array') }
+ if (!Array.isArray(queries) || queries.length < 1 || queries.length > 10 || queries.some(query => typeof query !== 'string' || !query.trim())) {
+ throw new Error('--additional-queries requires one to ten nonempty strings')
+ }
+ if (!['deep-lite', 'deep', 'deep-reasoning'].includes(args.type)) {
+ throw new Error('--additional-queries requires --type deep-lite, deep, or deep-reasoning')
+ }
+ research.additionalQueries = queries
+ }
+ return research
+}
+
const args = parseArgs(rawArgs)
const [cmd, ...rest] = args._
@@ -80,7 +112,7 @@ async function main() {
case 'search': {
const query = args.query || rest.join(' ')
if (!query) { result = { error: '--query required' }; break }
- const body = { query }
+ const body = { query, ...buildResearch(args) }
if (args.type) body.type = args.type
if (args.num) body.numResults = Number(args.num)
if (args.category) body.category = args.category
@@ -131,7 +163,7 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
- search: 'search --query <q> [--type neural|fast|auto|deep-lite|deep|deep-reasoning|instant] [--num <n>] [--category company|research paper|news|personal site|financial report|people] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--include-text <phrases>] [--exclude-text <phrases>] [--start-published <ISO>] [--end-published <ISO>] [--user-location <CC>] [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>]',
+ search: 'search --query <q> [--type neural|fast|auto|deep-lite|deep|deep-reasoning|instant] [--num <n>] [--category company|research paper|news|personal site|financial report|people] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--include-text <phrases>] [--exclude-text <phrases>] [--start-published <ISO>] [--end-published <ISO>] [--user-location <CC>] [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>] [--output-schema <JSON>] [--objective <text>] [--system-prompt <text>] [--additional-queries <JSON-array>]',
'find-similar': 'find-similar --url <url> [--num <n>] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--start-published <ISO>] [--end-published <ISO>] [--text] [--highlights] [--summary]',
contents: 'contents --urls <url1,url2> [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>]',
options: '--dry-run (preview request without sending)',
diff --git a/tools/integrations/exa.md b/tools/integrations/exa.md
index a17816d..68a3d0f 100644
--- a/tools/integrations/exa.md
+++ b/tools/integrations/exa.md
@@ -44,6 +44,18 @@ POST https://api.exa.ai/search
}
```
+### Structured research output
+
+[Exa Search](https://exa.ai/docs/reference/search) can synthesize a structured answer alongside search results and field-level grounding. Pass a JSON schema with root type `object` or `text`; the response's `output.content`, `output.grounding`, request ID and cost estimate are returned unchanged.
+
+```bash
+node tools/clis/exa.js search --query "newsletter platforms for small publishers" --type deep --output-schema '{"type":"object","properties":{"vendors":{"type":"array","items":{"type":"string"}}},"required":["vendors"]}' --objective "Compare vendors for a small publisher" --system-prompt "Use primary sources and identify missing evidence" --additional-queries '["newsletter pricing","newsletter deliverability"]' --highlights
+```
+
+`--additional-queries` takes a JSON array of one to ten nonempty query strings and requires `deep-lite`, `deep` or `deep-reasoning`. `--objective` describes the research goal (at most 4096 characters); `--system-prompt` supplies synthesis instructions. Ordinary search filters and content options still apply. Omit these flags to retain the existing request.
+
+The CLI validates JSON shape and supported root types; Exa validates the full schema, including its object property, nesting and array-item limits. This request is non-streaming, performs one search, and does not retry or invent citations. Inspect grounding and original sources before relying on generated content. Structured synthesis adds provider latency and can affect costs; the response estimate is not a billing guarantee. Preview with `--dry-run` before a paid request.
+
### Competitor Content Discovery
```bash