TypeError: 'Timeout' object cannot be interpreted as an integer in sync connect_tcp when a request carries a Timeout object in extensions

Open
#1,162 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@69hassn is already working on this.

Since Oct 13, 2026.

  • #1257 by @69hassn — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start in httpcore2/_backends/sync.py at SyncBackend.connect_tcp, then inspect the corresponding timeout handling in connect_unix_socket and start_tls. Reproduce the issue with the offline Request example and verify that a Timeout object is converted to an acceptable connection timeout or produces a clean error rather than a stdlib TypeError.

Written by the indexing model from the issue text.

Description

Summary

httpcore2/_backends/sync.py SyncBackend.connect_tcp passes the request timeout straight into socket.create_connection(address, timeout, ...). When a request's extensions["timeout"]["connect"] carries a Timeout object instead of a float (some SDK request builders do this — we hit it through the anthropic SDK path in hermes-agent), Python raises TypeError: 'Timeout' object cannot be interpreted as an integer before any network I/O, which the caller sees as a bogus APIConnectionError.

Reproducer (offline, no network needed)

import httpx2
from httpx2 import Timeout, Request, Client

r = Request("GET", "http://127.0.0.1:1/")
r.extensions["timeout"] = {"connect": Timeout(5.0)}
Client().send(r)

Result:

TypeError: 'Timeout' object cannot be interpreted as an integer
  File ".../httpcore2/_backends/sync.py", line 204, in connect_tcp
    sock = socket.create_connection(address, timeout, ...)
  File ".../socket.py", line 845, in create_connection
    sock.settimeout(timeout)

Environment

  • httpcore2 / httpx2 2.7.0
  • Python 3.11.13, macOS

Expected

socket.create_connection accepts only float | None. A Timeout object in the extensions should either be coerced (its numeric .connect attribute is the value) or surfaced as a clean error — not a stdlib TypeError from deep inside the connect path.

Suggested fix

In connect_tcp (and the same pattern in connect_unix_socket / start_tls):

if timeout is not None and not isinstance(timeout, (int, float)):
    timeout = getattr(timeout, "connect", None)

We applied exactly this as a local patch and it resolves the failure.

Dominant language
Python
Stars
1.6k
Forks
103
Avg merge
1d 19h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/httpx2

All issues in pydantic/httpx2

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.