40.5 Contract tests for schema compliance

Overview and links for this section of the guide.

The Contract

You need to prove that your prompt actually produces your schema.

The Tests

Write a unit test that runs against the real model (or a cached response):

test('model returns valid user schema', async () => {
  const response = await model.generate(prompt);
  const result = UserSchema.safeParse(response);
  expect(result.success).toBe(true);
});

Run this in CI/CD. If a model update (from Google) breaks your schema adherence, you need to know before you deploy.

Where to go next