Skip to content

Commit 2d2f4a2

Browse files
fix: handle 7-digit fractional seconds in _parse_iso for Python 3.10
Python 3.10's fromisoformat only supports up to 6 fractional digits (microseconds). Teams sends 7-digit timestamps (e.g., .4513813). Truncate extra digits before parsing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e425260 commit 2d2f4a2

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/chat_sdk/types.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@
2424
def _parse_iso(s: str) -> datetime:
2525
"""Parse ISO 8601 datetime string, supporting Python 3.10+.
2626
27-
Python 3.10's ``fromisoformat`` doesn't accept the ``Z`` suffix.
27+
Handles two Python 3.10 limitations:
28+
- ``Z`` suffix not accepted (replaced with ``+00:00``)
29+
- Fractional seconds limited to 6 digits (truncated from 7+)
2830
"""
29-
return datetime.fromisoformat(s.replace("Z", "+00:00"))
31+
import re
32+
33+
s = s.replace("Z", "+00:00")
34+
# Python 3.10 only supports up to 6 fractional digits (microseconds).
35+
# Truncate any extra digits (e.g., Teams sends 7-digit nanosecond timestamps).
36+
s = re.sub(r"(\.\d{6})\d+", r"\1", s)
37+
return datetime.fromisoformat(s)
3038

3139

3240
# =============================================================================

0 commit comments

Comments
 (0)