Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""Smoke tests for the xAI Responses API (grok-4.3): create, stream events, previous_response_id, store=false + encrypted reasoning, GET/DELETE/input_items, compact, json_schema."""2from __future__ import annotations34import json56import pytest789def _text(body):10 return next((c["text"] for o in body["output"] if o["type"] == "message" for c in o["content"] if c["type"] == "output_text"), None)111213def test_create_retrieve_input_items_delete(xai, models):14 st, r1, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Reply with OK.", "max_output_tokens": 2000}, est_cost_usd=0.0005, note="test_responses minimal")15 assert st == 200 and r1["object"] == "response" and r1["status"] == "completed" and r1["store"] is True16 assert "OK" in _text(r1)17 assert r1["usage"]["output_tokens_details"]["reasoning_tokens"] >= 0 and "cached_tokens" in r1["usage"]["input_tokens_details"]18 st, r2, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Say it again.", "previous_response_id": r1["id"], "max_output_tokens": 2000}, est_cost_usd=0.0005, note="test_responses previous_response_id")19 assert st == 200 and r2["previous_response_id"] == r1["id"]20 st, got, _ = xai("GET", f"/v1/responses/{r1['id']}", note="test_responses get")21 assert st == 200 and got["id"] == r1["id"]22 st, items, _ = xai("GET", f"/v1/responses/{r1['id']}/input_items", note="test_responses input_items")23 assert st == 200 and items["object"] == "list" and items["data"][0]["role"] == "user" and items["has_more"] is False24 for rid in (r1["id"], r2["id"]):25 st, d, _ = xai("DELETE", f"/v1/responses/{rid}", note="test_responses delete")26 assert st == 200 and d == {"id": rid, "object": "response", "deleted": True}27 st, _, _ = xai("GET", f"/v1/responses/{r1['id']}", note="test_responses get after delete")28 assert st == 404293031def test_stream_event_sequence(xai, models):32 st, lines, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Reply with OK.", "stream": True, "store": False, "max_output_tokens": 2000}, stream=True, est_cost_usd=0.0005, note="test_responses stream")33 assert st == 20034 events = [l[6:].strip() for l in lines if l.startswith("event:")]35 assert events[0] == "response.created" and events[-1] == "response.completed"36 assert "response.output_text.delta" in events and "response.output_item.done" in events373839def test_store_false_and_encrypted_reasoning_replay(xai, models):40 st, r, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Reply with OK.", "store": False, "include": ["reasoning.encrypted_content"], "max_output_tokens": 2000}, est_cost_usd=0.0005, note="test_responses store=false encrypted")41 assert st == 200 and r["store"] is False42 reasoning = [o for o in r["output"] if o["type"] == "reasoning"]43 if reasoning: # the reasoning item is sometimes omitted by the API44 assert reasoning[0].get("encrypted_content")45 st, r2, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": [{"role": "user", "content": "Reply with OK."}] + r["output"] + [{"role": "user", "content": "Again."}], "store": False, "max_output_tokens": 2000}, est_cost_usd=0.0005, note="test_responses replay output items")46 assert st == 200 and _text(r2)474849def test_text_format_json_schema(xai, models):50 schema = {"type": "object", "properties": {"answer": {"type": "string"}}, "required": ["answer"], "additionalProperties": False}51 st, r, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Reply with answer OK.", "store": False, "max_output_tokens": 2000, "text": {"format": {"type": "json_schema", "name": "ok", "strict": True, "schema": schema}}}, est_cost_usd=0.0005, note="test_responses json_schema")52 assert st == 200 and json.loads(_text(r))["answer"]535455def test_unsupported_fields(xai, models):56 st, b, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Reply with OK.", "background": True, "store": False}, est_cost_usd=0, note="test_responses background")57 assert st == 400 and "background" in json.dumps(b)58 st, b, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": "Reply with OK.", "instructions": "x", "previous_response_id": "resp_x", "store": False}, est_cost_usd=0, note="test_responses instructions+previous")59 assert st in (400, 404)606162@pytest.mark.run_expensive_tests63def test_compact_roundtrip(xai, models):64 hist = [{"role": "system", "content": "You are terse."}, {"role": "user", "content": "Say A."}, {"role": "assistant", "content": "A."}, {"role": "user", "content": "Say B."}, {"role": "assistant", "content": "B."}]65 st, c, _ = xai("POST", "/v1/responses/compact", {"model": models["xai"], "input": hist}, est_cost_usd=0.002, note="test_responses compact")66 assert st == 200 and c["object"] == "response.compaction" and c["output"][0]["type"] == "compaction" and c["usage"]["dropped_message_count"] >= 167 st, r, _ = xai("POST", "/v1/responses", {"model": models["xai"], "input": c["output"] + [{"role": "user", "content": "Which letters did I ask for? Three words."}], "store": False, "max_output_tokens": 2000}, est_cost_usd=0.001, note="test_responses after compact")68 assert st == 200 and _text(r)69