Files
stpweb/python/app/claude.py
T
adamfandClaude Opus 5.5 307268fbea Put every Claude call behind one helper in the Python service
rowing.py and jobs.py repeated the same call, error mapping, fence
stripping and validation; that now lives in claude.ask_json, and the
internal routes share one wrapper and one exception handler in main.py.

Add /internal/email/classify, which takes over the email pipeline's
Claude call from the Go backend (prompt ported verbatim, plus a sentence
fencing the email off as data).

The internal-only guard is now a dependency, so a request that came
through nginx gets 404 even when its body is invalid, where it used to
get 422.

requirements-dev.txt adds pytest and httpx on top of requirements.txt so
the tests run on a host; the image still installs only requirements.txt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 14:39:18 +01:00

133 lines
4.9 KiB
Python

"""The Anthropic client and the one call every Claude caller here makes.
`rowing` (a photo in, three numbers out), `jobs` (page text in, a few fields
out) and `email_classify` (an email in, a verdict out) are the same shape of
call: one prompt asking for bare JSON, one reply to parse. That whole sequence
is `ask_json` below, so a caller is a prompt, a pydantic model and three error
strings, and the handling around the call cannot drift between them. Also
here: the lazily built client and the two exception types `main` maps to 502
and 503.
Like the modules that use it, this never imports FastAPI.
"""
import functools
import logging
import os
from typing import Callable, Optional, TypeVar
import anthropic
from pydantic import BaseModel, ValidationError
log = logging.getLogger(__name__)
T = TypeVar("T", bound=BaseModel)
MODEL = "claude-haiku-4-5"
# Seconds per attempt. The SDK retries twice on top (429/5xx/connection), so
# the worst case is roughly three of these plus backoff; the Go callers' own
# timeout is set above that so they see this side's error, not their own.
TIMEOUT_S = 30.0
class ReadError(Exception):
"""Claude could not produce the answer; the message is safe to show the admin."""
class NotConfigured(ReadError):
"""CLAUDE_API_KEY is not set in this container."""
@functools.lru_cache(maxsize=1)
def client() -> anthropic.Anthropic:
"""Build the Anthropic client on first use.
Lazy so that a missing key only breaks the Claude features: the hotel
finder must still boot without it. The key is passed explicitly because the
stack's variable is CLAUDE_API_KEY, not the ANTHROPIC_API_KEY the SDK looks
for.
"""
key = os.environ.get("CLAUDE_API_KEY", "")
if not key:
raise NotConfigured("CLAUDE_API_KEY is not set")
return anthropic.Anthropic(api_key=key, timeout=TIMEOUT_S)
def strip_markdown_fence(raw: str) -> str:
"""Remove a surrounding markdown code fence from a model's response.
The prompts ask for bare JSON and the model fences it anyway often enough
to matter. "```json" is tried before a plain "```", and unfenced input
comes back unchanged apart from surrounding whitespace. This is the only
copy: the Go backend had its own while it still called Claude itself, and
every Claude call now goes through this service.
"""
raw = raw.strip()
raw = raw.removeprefix("```json")
raw = raw.removeprefix("```")
raw = raw.removesuffix("```")
return raw.strip()
def ask_json(
model_cls: type[T],
*,
max_tokens: int,
messages: list,
system: Optional[str] = None,
failed: str,
empty: str,
unparseable: str,
get_client: Optional[Callable[[], anthropic.Anthropic]] = None,
) -> T:
"""Send one prompt that asks for a bare JSON object and validate the reply.
The three message arguments are what the caller's user ends up reading:
the Go backend relays a ReadError's text verbatim, so each caller words
its own and nothing from the API or the model's reply leaks into it. The
detail goes to the log instead.
Args:
model_cls: the pydantic model the reply must validate as; its field
names are the keys the caller's prompt asks for.
max_tokens: ceiling on the reply.
messages: the `messages` list, passed to the API as is.
system: the system prompt, left out of the request when None.
failed: ReadError text when the API call itself failed.
empty: ReadError text when the reply has no text block.
unparseable: ReadError text when the reply is not JSON, or is JSON
that `model_cls` rejects.
get_client: returns the Anthropic client. Callers pass their own
module-level `_client` so that a test can replace the client for
one module; resolved here, per call, when not given.
Raises:
NotConfigured: no API key.
ReadError: with one of the three messages above.
"""
request = {"model": MODEL, "max_tokens": max_tokens, "messages": messages}
if system is not None:
request["system"] = system
# Outside the try: NotConfigured is not an API failure and must reach
# `main` as itself, to become the 503.
api = (get_client or client)()
try:
message = api.messages.create(**request)
except anthropic.APIStatusError as e:
log.warning("Claude API error %s: %s", e.status_code, e.message)
raise ReadError(failed) from e
except anthropic.APIConnectionError as e:
log.warning("Claude API unreachable: %s", e)
raise ReadError(failed) from e
text = next((block.text for block in message.content if block.type == "text"), None)
if text is None:
raise ReadError(empty)
try:
return model_cls.model_validate_json(strip_markdown_fence(text))
except ValidationError as e:
log.warning("unparseable model output %r: %s", text, e)
raise ReadError(unparseable) from e