fix watsonx datetime conversion issue py3.10 (#12339)

Got ValueError on Python3.10 because datetime.fromisoformat could not handle 'Z' -> '+00:00' at the end of a timestamp. Instead, try with the previous behavior, except ValueError, then attempt removing Z, and adding +00:00 to support Python3.10
This commit is contained in:
isaken 2025-07-05 18:01:11 +02:00 • committed by GitHub
parent 738db9336e
commit 524f10bf6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -300,9 +300,14 @@ class IBMWatsonXAIConfig(IBMWatsonXMixin, BaseConfig):
json_resp["results"][0]["stop_reason"]
)
if json_resp.get("created_at"):
model_response.created = int(
datetime.fromisoformat(json_resp["created_at"]).timestamp()
)
try:
created_datetime = datetime.fromisoformat(json_resp["created_at"])
except ValueError:
# datetime.fromisoformat cannot handle 'Z' in Python 3.10
created_datetime = datetime.fromisoformat(
f'{json_resp["created_at"].rstrip("Z")}+00:00'
)
model_response.created = int(created_datetime.timestamp())
else:
model_response.created = int(time.time())
usage = Usage(