Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""Cheap deliberate error tests for the Anthropic API (nothing is billed: every request is rejected). Verified live 2026-09-18."""2from __future__ import annotations34import pytest56MSG = "/v1/messages"789def _body(model, **kw):10 b = {"model": model, "max_tokens": 8, "messages": [{"role": "user", "content": "Reply with OK."}]}11 b.update(kw)12 return b131415def _assert_error(st, body, hdrs, status, etype):16 assert st == status, body17 assert body["type"] == "error" and body["error"]["type"] == etype18 assert body["request_id"].startswith("req_")19 lower = {k.lower(): v for k, v in hdrs.items()}20 assert lower["request-id"] == body["request_id"]21 assert lower.get("x-should-retry") == "false" # LIVE_DISCOVERED header on every 4xx22 return body["error"]["message"]232425def test_404_unknown_model(anthropic, models):26 st, body, hdrs = anthropic("POST", MSG, _body("claude-does-not-exist"), note="test_errors 404 model")27 assert _assert_error(st, body, hdrs, 404, "not_found_error").startswith("model:")282930def test_400_missing_max_tokens(anthropic, models):31 b = _body(models["anthropic"]); del b["max_tokens"]32 st, body, hdrs = anthropic("POST", MSG, b, note="test_errors 400 max_tokens")33 assert "max_tokens" in _assert_error(st, body, hdrs, 400, "invalid_request_error")343536def test_400_max_tokens_over_model_max(anthropic, models):37 st, body, hdrs = anthropic("POST", MSG, _body(models["anthropic"], max_tokens=1_000_000), note="test_errors 400 max_tokens too large")38 assert "64000" in _assert_error(st, body, hdrs, 400, "invalid_request_error")394041@pytest.mark.parametrize("field,value,fragment", [("temperature", 5, "range"), ("top_k", "abc", "integer"), ("messages", [], "at least one")])42def test_400_validation(anthropic, models, field, value, fragment):43 st, body, hdrs = anthropic("POST", MSG, _body(models["anthropic"], **{field: value}), note=f"test_errors 400 {field}")44 assert fragment in _assert_error(st, body, hdrs, 400, "invalid_request_error")454647def test_400_temperature_and_top_p_together(anthropic, models):48 st, body, hdrs = anthropic("POST", MSG, _body(models["anthropic"], temperature=0.5, top_p=0.9), note="test_errors 400 temperature+top_p")49 assert "cannot both be specified" in _assert_error(st, body, hdrs, 400, "invalid_request_error")505152def test_401_bad_key(anthropic, models):53 st, body, hdrs = anthropic("POST", MSG, _body(models["anthropic"]), extra_headers={"x-api-key": "invalid-key-placeholder"}, note="test_errors 401")54 assert st == 401 and body["error"]["type"] == "authentication_error"555657def test_400_invalid_version_header(anthropic, models):58 st, body, hdrs = anthropic("POST", MSG, _body(models["anthropic"]), extra_headers={"anthropic-version": "2020-01-01"}, note="test_errors version")59 assert "anthropic-version" in _assert_error(st, body, hdrs, 400, "invalid_request_error")606162def test_400_unknown_beta_header(anthropic, models):63 st, body, hdrs = anthropic("POST", MSG, _body(models["anthropic"]), beta="does-not-exist-2099-01-01", note="test_errors beta")64 assert "anthropic-beta" in _assert_error(st, body, hdrs, 400, "invalid_request_error")656667def test_400_prefill_rejected_on_claude_5(anthropic):68 st, body, hdrs = anthropic("POST", MSG, {"model": "claude-sonnet-5", "max_tokens": 8, "messages": [69 {"role": "user", "content": "Say OK."}, {"role": "assistant", "content": "The word is:"}]}, note="test_errors prefill sonnet-5")70 assert "prefill" in _assert_error(st, body, hdrs, 400, "invalid_request_error")717273def test_400_legacy_complete_deprecated(anthropic):74 st, body, hdrs = anthropic("POST", "/v1/complete", {"model": "claude-2.1", "max_tokens_to_sample": 8, "prompt": "\n\nHuman: hi\n\nAssistant:"}, note="test_errors /v1/complete")75 assert "deprecated" in _assert_error(st, body, hdrs, 400, "invalid_request_error")76