Thanks to visit codestin.com
Credit goes to github.com

Skip to content

JSON schema for array of objects #15

@yohanc3

Description

@yohanc3

Hi, I'm trying out the API for something I'm building, and I'm having issues with json responses.

My code:

		const completion = await client.chat.completions.create({
			messages: [{ content: promptContent, role: 'user' }],
			model: 'Llama-4-Scout-17B-16E-Instruct-FP8',
			response_format: {
				type: 'json_schema',
				json_schema: {
					name: 'similarity_scores',
					schema: {
						type: 'array',
						items: {
							type: 'object',
							properties: {
								productID: { type: 'string' },
								similarity: { type: 'number', minimum: 0, maximum: 10 },
							},
							required: ['productID', 'similarity'],
							additionalProperties: false,
						},
					},
				},
			},
		});

The api response:

BadRequestError: 400 {"title":"Bad request","detail":"Response format 'json_schema' is an invalid JSON schema object","status":400}
    at APIError.generate (file:///Users/yohance/Documents/coding/digreddit/lead_evaluator/.wrangler/tmp/dev-ow855L/index.js:987:14)
    at _LlamaAPIClient.makeStatusError (file:///Users/yohance/Documents/coding/digreddit/lead_evaluator/.wrangler/tmp/dev-ow855L/index.js:2249:21)
    at _LlamaAPIClient.makeRequest (file:///Users/yohance/Documents/coding/digreddit/lead_evaluator/.wrangler/tmp/dev-ow855L/index.js:2380:24)
    at async calculateSimilarity (file:///Users/yohance/Documents/coding/digreddit/lead_evaluator/.wrangler/tmp/dev-ow855L/index.js:2608:24)
    at async Object.fetch (file:///Users/yohance/Documents/coding/digreddit/lead_evaluator/.wrangler/tmp/dev-ow855L/index.js:5234:24)
    at async drainBody (file:///Users/yohance/Documents/coding/digreddit/lead_evaluator/.wrangler/tmp/dev-ow855L/index.js:5257:12) {
  status: 400,
  headers: Headers,
  error: Object,
  stack: Error: 400 {"title":"Bad request","detail":"Respon…luator/.wrangler/tmp/dev-ow855L/index.js:5257:12),
  message: 400 {"title":"Bad request","detail":"Response form…' is an invalid JSON schema object","status":400}
}

Let me know if the issue resides on my end, and a possible fix. I couldn't find any related information in the only docs I found related to json schema responses - they only mention how to manage objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions