Skip to content

Commit 02c65b4

Browse files
fern-supportclaude
andcommitted
fix: Improve error handling for response extensions and session tokens
- Use .get() for extensions['endpoint'] with safe fallback - Add FileNotFoundError handling for expired OCI session tokens - Validate key_file presence in session auth config - Document session token expiry limitation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 115468d commit 02c65b4

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/cohere/oci_client.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,27 @@ def map_request_to_oci(
272272
)
273273
elif "security_token_file" in oci_config:
274274
# Session-based authentication with security token (fallback if no user field)
275+
# Note: Session tokens expire (typically ~1 hour). If expired, re-run:
276+
# oci session authenticate --profile <profile>
275277
token_file_path = os.path.expanduser(oci_config["security_token_file"])
276-
with open(token_file_path, "r") as f:
277-
security_token = f.read().strip()
278+
try:
279+
with open(token_file_path, "r") as f:
280+
security_token = f.read().strip()
281+
except FileNotFoundError:
282+
raise ValueError(
283+
f"OCI session token file not found: {token_file_path}. "
284+
"Your session may have expired. Re-authenticate with: "
285+
"oci session authenticate"
286+
)
278287

279288
# Load private key using OCI's utility function
280-
private_key = oci.signer.load_private_key_from_file(oci_config["key_file"])
289+
key_file = oci_config.get("key_file")
290+
if not key_file:
291+
raise ValueError(
292+
"OCI config profile is missing 'key_file'. "
293+
"Session-based auth requires a key_file entry in your OCI config profile."
294+
)
295+
private_key = oci.signer.load_private_key_from_file(key_file)
281296

282297
signer = oci.auth.signers.SecurityTokenSigner(
283298
token=security_token,
@@ -362,7 +377,9 @@ def map_response_from_oci() -> EventHook:
362377
"""
363378

364379
def _hook(response: httpx.Response) -> None:
365-
endpoint = response.request.extensions["endpoint"]
380+
endpoint = response.request.extensions.get("endpoint")
381+
if endpoint is None:
382+
return # Request hook didn't run; pass response through unchanged
366383
is_stream = response.request.extensions.get("is_stream", False)
367384

368385
output: typing.Iterator[bytes]

0 commit comments

Comments
 (0)