When transforming data to/from the API, always implement the backend’s explicit contract: ordering invariants, exact serialization, correct parsing by response format/content-type, endpoint-appropriate headers, and backend-specific URL/auth/version rules.
Apply these checks consistently:
api_type.Example (response-format-safe parsing + invariant-preserving payload building):
def get_response_input_items(response):
# preserve required reasoning->assistant consecutive pairing
items = []
for output_item in response.output:
items.append(output_item.model_dump(exclude_unset=True))
return items
def parse_response(rbody: str, headers: dict[str,str], response_format):
if response_format == "json" or headers.get("Content-Type") == "application/json":
return json.loads(rbody)
return rbody # text/srt/vtt
Rule of thumb: if the contract can be stated as “must be ordered X”, “must be JSON vs text Y”, or “must send header key Z / URL path P”, encode it as logic + validation—not as an assumption.
Enter the URL of a public GitHub repository