Gemini API (python) stalled for a long time if compared with direct Postman REST calls

Hi All,

I am eperiencing delays when using the latest (1.47.0) google-genai Python client. The same endpoint takes <10s in Postman, whereas it exceeds 50s using the Python client. It appears somehow the request is stalled, as if there was a SYN/ACK issue at network level. I tried multiple client configurations and both locally and from GCP.

Hi @Paolo_Ursino
Welcome to the AI Forum!!!

Thank you for bringing this to our attention.
Could you please share the full payload details along with some sample of the code which you are using? We would like to reproduce the issue.

Hi Shivam,

Thanks for replying.

Any payload has the same issue. However, the same call via postman (and same API key) doesn’t.

See the snippet below:
import os

import httpx, ssl

from datetime import datetime

import openai

from google import genai

from google.genai import types

import tiktoken

from constants import *

from logger import setup_logger

# Initialize logger

logger = setup_logger(“medsii-ai.openai_func”)

def trace_event(label: str):

*# httpx event hooks receive the request/response; accept \*args to be liberal*

*def hook(\*\_args, \*\*\_kwargs):*

    *print(f"{datetime.now().strftime('%H:%M:%S')}  {label}")*

*return hook*

# NOTE: The client automatically looks for the GEMINI_API_KEY environment variable.

try:

*GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')*



*# Force TLS 1.2, avoid TLS 1.3 negotiation stalls*

*ssl_ctx = ssl.create_default_context()*

*ssl_ctx.minimum_version = ssl.TLSVersion.TLSv1_2*

*ssl_ctx.maximum_version = ssl.TLSVersion.TLSv1_2*    



*client = genai.Client(*

  *api_key=GEMINI_API_KEY,*

  *http_options=types.HttpOptions(*

    *client_args={*

        *"event_hooks": {*

            *"request": \[trace_event("→ request start")\],*

            *"response": \[trace_event("← response headers")\]*

        *},*

        *"timeout": httpx.Timeout(connect=5.0, read=20.0, write=10.0, pool=5.0),*

        *"trust_env": False, # Good to keep*



        *# --- THIS IS THE TEST ---*

        *# --- CRITICAL FIX 2: Explicitly set proxies to an empty dictionary ---*

        *# This is the most forceful way to tell httpx to NOT use a proxy* 

        *# for ANY traffic, bypassing even low-level system injections.*

        *"proxies": {},*

        *# This is NOT for production.*

        *# If this is fast, the problem is 100% certificate verification.*

        *"verify": False*

    *},*

*))*      
*models = client.models.list()  # this returns a Pager (iterable)*

*for m in models:*

    *print(m.name)

The time between request and response headers varies from 30 to 60 srec. You will notice various attempts to bypass proxies etc. in the httpOptions. But even with the default client configuration the issue is the same.

Kind Regards,

Paolo

Hello,

Based on the details provided, we attempted to reproduce the issue, but the service appears to be functioning as expected.

We tested using three methods: Postman, the Python SDK, and cURL commands in the terminal. All three methods resulted in roughly the same processing time (~37 seconds). If you are still experiencing this issue, please provide the exact prompts and configuration where you observed the discrepancy in inference time between the SDK and REST API calls so we can assist further.