Docs/Recipes

Get JSON back, not prose

Pass a JSON Schema and use reply.json straight in your app.

extract.js
const { json } = await charge.ai.chat('Pull out the bill details', {
  files: [photo],
  schema: {
    type: 'object',
    required: ['total', 'due'],
    properties: {
      total: { type: 'number' },
      due: { type: 'string', description: 'Due date, YYYY-MM-DD' },
      items: { type: 'array', items: { type: 'string' } },
    },
  },
})
form.amount.value = json.total
  • The reply is checked against your schema before it reaches you. If it doesn't match, the model is asked once more, and both tries are charged.
  • If it still doesn't match, the call fails with invalid_json. Keep schemas small and clear, and describe fields in description.
  • Schema replies aren't streamed and always run in the cloud.

Read next

Still stuck? Talk to us and we'll help.