Repository navigation
Description
API key is echoed into the exception text, and an unusable key is retried
Version: typesafe-sdk 0.7.0 (2ce5c65), Python 3.12, macOS
What happens
An explicit api_key that ends in a newline (the usual result of reading a key from a file or a Kubernetes secret mount) is never sent. httpx rejects the header locally. The SDK then:
- reports it as
TypeSafeAPIConnectionErrorwith the full key in the message:Connection error: Illegal header value b'Bearer ts_live_...\n', which then lands in logs and error trackers; - retries the request twice (about 1.3 s spent), although nothing can succeed.
api_key="" behaves the same way (Illegal header value b'Bearer ', retried), instead of failing fast like a missing TYPESAFE_API_KEY does.
Keys from TYPESAFE_API_KEY are stripped (_core/config.py, _resolve_env); an explicit api_key is used as-is (config.py:21,48). _core/transport.py:79-83 maps httpx.LocalProtocolError to a connection error, and api_connection_error=True makes it retryable.
The SDK clearly means to keep the key out of output (api_key is repr=False, and SECRET_HEADERS redacts it in logs), so this looks unintended.
Reproduce (local server, no real key or network needed)
import http.server, threading, time
from typesafe_sdk import TypeSafeClient
hits = []
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
hits.append(self.path); self.send_response(200)
self.send_header("Content-Type", "application/json"); self.end_headers(); self.wfile.write(b'{"models":[]}')
def log_message(self, *a): pass
srv = http.server.HTTPServer(("127.0.0.1", 0), H)
threading.Thread(target=srv.serve_forever, daemon=True).start()
for key in ["ts_live_EXAMPLEKEY\n", ""]:
t = time.perf_counter()
try:
TypeSafeClient(api_key=key, base_url=f"http://127.0.0.1:{srv.server_port}").models.list()
except Exception as e:
print(repr(key), type(e).__name__, str(e), "| server hits:", len(hits), "| %.1fs" % (time.perf_counter() - t))Output:
'ts_live_EXAMPLEKEY\n' TypeSafeAPIConnectionError Connection error: Illegal header value b'Bearer ts_live_EXAMPLEKEY\n' | server hits: 0 | 1.3s
'' TypeSafeAPIConnectionError Connection error: Illegal header value b'Bearer ' | server hits: 0 | 1.3s
Suggested fix (about 10 lines)
- Strip an explicit
api_keythe same way the environment value is stripped. - Reject an empty key, or a key containing control characters, with a
TypeSafeErrorwhose message does not include the key. - Do not classify
httpx.LocalProtocolErroras a retryable connection error: it is raised before anything is sent.
Happy to test a fix against the repro above.
Activity
danielgafni commentedon Sep 21, 2026
Fixed in https://github.com/typesafe-ai/typesafe-sdk-python/releases/tag/v0.7.1