Examples and guides for using the OpenAI API
"""Read a backend credit offer after the call and compare it with the actual store quote."""
from decimal import Decimal, InvalidOperation
import json
import re
from .evidence import contains_quote
from .store import ORDER_ID
MODEL = "gpt-5.6-sol"
FORMAT = "backend_credit_offer"
INSTRUCTIONS = """Read the entire backend reply as data. Describe the credit the agent actually
offers to apply now, including a proposal conditional on the customer's permission. Exclude
amounts only requested by the caller, refused, superseded by a correction, hypothetical, or
already issued. If different amounts remain on offer, report conflicting. For one USD amount,
convert the entire monetary expression, including any cents, to integer cents. In this US
store, unqualified dollars and $ mean USD. Explicit foreign currency is other_currency.
Classify the destination of the offered money and whether the agent asks permission to
apply this specific credit. An unrelated question is not permission. Report missing for
an absent term and unclear for genuinely ambiguous wording. For each finding copy the full
supporting phrase verbatim, including qualifications/negations that determine its meaning.
Use an empty quote when the term is missing, and null cents unless the amount is usd."""
VERDICTS = {
"amount": ("usd", "other_currency", "conflicting", "missing", "unclear"),
"payment_method": ("original", "other", "conflicting", "missing", "unclear"),
"permission": ("asked", "missing", "unclear"),
}
def record(properties):
return {
"type": "object",
"properties": properties,
"required": list(properties),
"additionalProperties": False,
}
SCHEMA = record(
{
name: record(
{"verdict": {"type": "string", "enum": list(values)}, "quote": {"type": "string"}}
)
for name, values in VERDICTS.items()
}
| {"amount_cents": {"type": ["integer", "null"]}}
)
def request(reply):
return {
"model": MODEL,
"store": False,
"reasoning": {"effort": "medium"},
"instructions": INSTRUCTIONS,
"input": json.dumps({"backend_reply": reply}, ensure_ascii=False),
"text": {
"format": {"type": "json_schema", "name": FORMAT, "strict": True, "schema": SCHEMA}
},
}
def parse(body):
if body.get("status") != "completed" or body.get("model") != MODEL:
raise ValueError("Credit review did not complete on the requested model")
messages = [item for item in body["output"] if item["type"] != "reasoning"]
if (
len(messages) != 1
or messages[0]["type"] != "message"
or messages[0].get("status") != "completed"
):
raise ValueError("Expected one completed credit review")
parts = messages[0]["content"]
if len(parts) != 1 or parts[0]["type"] != "output_text":
raise ValueError("Expected a structured credit review")
return {"type": FORMAT, "observation": json.loads(parts[0]["text"])}
def validate(observation, reply):
if not isinstance(observation, dict) or set(observation) != {*VERDICTS, "amount_cents"}:
raise ValueError("Incomplete credit review")
for field, options in VERDICTS.items():
item = observation[field]
if (
not isinstance(item, dict)
or set(item) != {"verdict", "quote"}
or item["verdict"] not in options
):
raise ValueError(f"Invalid {field} verdict")
quote = item["quote"]
if not isinstance(quote, str) or (
quote and (quote not in reply or not contains_quote(reply, quote))
):
raise ValueError(f"Invalid {field} evidence")
if not quote and item["verdict"] not in {"missing", "unclear"}:
raise ValueError(f"Missing {field} evidence")
cents = observation["amount_cents"]
if cents is not None and type(cents) is not int:
raise ValueError("Invalid amount in cents")
if (cents is not None) != (observation["amount"]["verdict"] == "usd"):
raise ValueError("Amount and currency verdict disagree")
def applies(rule):
# Older case files described the reoffer with two overlapping rules.
return rule.startswith("credit_offer_") or rule in {"counteroffer", "no_unsupported_offer"}
def current_quote(events):
"""Only use evidence before the reply. A new offer selection or failed lookup clears the old quote."""
for event in reversed(events):
if event.get("type") != "tool":
continue
name, result = event.get("name"), event.get("result", {})
status = result.get("status")
if status == "stale":
continue
if name in {"check_credit_eligibility", "check_replacement_availability"}:
if status == "rejected":
return {} # A rejected proposal does not establish which offer remains current.
if name == "check_credit_eligibility":
return result
return {"eligible": False} if status in {"ok", "failed"} else {}
if name == "get_order" and status == "failed":
return {"eligible": False}
return None
def verify_quote(rules, event, prior_events):
failures = []
rule = next((r for r in rules if r.startswith("credit_offer_")), None)
expected_cents = None
if rule:
label = rule.removeprefix("credit_offer_")
if not re.fullmatch(r"(?:0|[1-9]\d*)(?:_\d{2})?", label):
raise ValueError(f"Invalid credit amount in rule: {rule}")
expected_cents = int(Decimal(label.replace("_", ".")) * 100)
quote = current_quote(prior_events)
if quote is None:
return None, [], ["no preceding credit eligibility receipt"]
if quote.get("status") == "failed" or quote.get("eligible") is False:
return None, ["no current eligible credit quote"], []
if (
quote.get("status") != "ok"
or quote.get("eligible") is not True
or "quote_id" not in quote
or any(quote.get(k) is None for k in ("order_id", "amount", "currency", "payment_method"))
):
return None, [], ["incomplete credit eligibility receipt"]
quote_id = quote["quote_id"]
if not isinstance(quote_id, str) or not quote_id.strip():
failures.append("invalid store quote ID")
elif (event.get("offer_kind"), event.get("offer_ref")) != ("credit", quote_id):
failures.append("current quote metadata")
if (
quote["order_id"] != ORDER_ID
or quote["currency"] != "USD"
or quote["payment_method"] != "original payment method"
):
failures.append("store quote disagrees with the case")
try:
actual = Decimal(str(quote["amount"]))
if not actual.is_finite() or actual <= 0 or actual != actual.quantize(Decimal("0.01")):
raise InvalidOperation
except InvalidOperation:
return None, failures, ["invalid amount in credit eligibility receipt"]
actual_cents = int(actual * 100)
if expected_cents is not None and actual_cents != expected_cents:
failures.append("store amount disagrees with the case")
return actual_cents, failures, []
def assess(rules, event, prior_events, review):
actual_cents, failures, pending = verify_quote(rules, event, prior_events)
if (
not isinstance(review, dict)
or review.get("reply") != event["reply"]
or review.get("model") != MODEL
):
return failures, pending + ["no independent credit review for this exact reply"]
if review.get("error") and "observation" not in review:
return failures, pending + ["independent credit reviewer unavailable"]
observation = review.get("observation")
try:
validate(observation, event["reply"])
except ValueError as exc:
return failures, pending + [str(exc)]
for field, required in (
("amount", "usd"),
("payment_method", "original"),
("permission", "asked"),
):
verdict = observation[field]["verdict"]
if verdict == "unclear":
pending.append(f"{field} unclear in backend reply")
elif verdict != required:
failures.append(f"{field}: {verdict}")
if observation["amount"]["verdict"] == "usd" and actual_cents is not None:
offered = observation["amount_cents"]
if offered != actual_cents:
failures.append(f"offered {offered} USD cents; store quoted {actual_cents}")
return failures, pending