twitter_openapi_python icon indicating copy to clipboard operation
twitter_openapi_python copied to clipboard

🧩 Bug Report β€” KeyError: 'x-connection-hash' in `api_utils.py` (v0.0.41–0.0.42)

Open kiko923 opened this issue 2 weeks ago β€’ 0 comments
trafficstars

πŸ› Description

When using twitter_openapi_python v0.0.41 or v0.0.42,
API calls such as get_tweet_result_by_rest_id() raise a KeyError because
Twitter no longer returns the header "x-connection-hash".

Error traceback:

File ".../site-packages/twitter_openapi_python/core/api_utils.py", line 64, in build_header
    connection_hash = headers["x-connection-hash"]
KeyError: 'x-connection-hash'

🧾 Affected File

twitter_openapi_python/core/api_utils.py

Function name:

def build_header(headers: Dict[str, str]) -> ApiUtilsHeader:

βš™οΈ Environment

  • OS: Linux / Windows
  • Python: 3.11.x
  • Package Version: 0.0.41 / 0.0.42
  • Twitter API change: The header x-connection-hash no longer exists in Twitter’s HTTP response.

βœ… Suggested Fix

Replace strict key lookups with safe .get() calls to avoid KeyError.
Proposed fixed code:

def build_header(headers: Dict[str, str]) -> ApiUtilsHeader:
    return ApiUtilsHeader(
        raw=headers,
        connection_hash=headers.get("x-connection-hash", ""),  # <-- Safe access
        content_type_options=headers.get("x-content-type-options", ""),
        frame_options=headers.get("x-frame-options", ""),
        rate_limit_limit=int(headers.get("x-rate-limit-limit", 0)),
        rate_limit_remaining=int(headers.get("x-rate-limit-remaining", 0)),
        rate_limit_reset=int(headers.get("x-rate-limit-reset", 0)),
        response_time=int(headers.get("x-response-time", 0)),
        tfe_preserve_body=headers.get("x-tfe-preserve-body") == "true",
        transaction_id=headers.get("x-transaction-id", ""),
        twitter_response_tags=headers.get("x-twitter-response-tags", ""),
        xss_protection=int(headers.get("x-xss-protection", 0)),
    )

πŸ§ͺ Test Result

After applying this fix, all Twitter API endpoints (e.g. get_tweet_result_by_rest_id, get_user_by_screen_name) worked normally again on both Linux and Windows with Python 3.11.x.


πŸ™ Request

Please include this fix in the next release of twitter_openapi_python
so that users of v0.0.41+ no longer encounter KeyError: 'x-connection-hash'.

Thanks for your work on maintaining this great library!

kiko923 avatar Nov 10 '25 03:11 kiko923