Use consistent, typed exceptions at the point of failure, with structured context, and never rely on unchecked/bypassed paths that convert errors into later crashes.
Apply:
OpenAIError) for all credential/installation/mode-resolution issues; wrap underlying exceptions via raise ... from exc.ContentFormatError) that includes actionable context (such as raw_content).raise NewError(...) from exc and don’t manually set __cause__.Example (parsing boundary):
try:
model_parse_json(MyModel, content) # or TypeAdapter(...).validate_json
except (pydantic.ValidationError, json.JSONDecodeError) as exc:
raise ContentFormatError(raw_content=content, error=exc) from exc
Example (polling boundary):
start = time.time()
while not until(response):
if time.time() - start > TIMEOUT_SECS:
raise OpenAIError("Polling timed out")
response = self.request(...)[0]
This reduces mystery failures (like KeyError) and makes errors actionable for users and tests.
Enter the URL of a public GitHub repository