Skip to content

requests has not attribute session #16035

Description

@sakshamgoyal-01

Determine this is the right repository

  • I determined this is the correct repository in which to report this bug.

Summary of the issue

Faulty logic results in a bug. Here is a fix:

.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py

 if use_mtls:
# Ensure the request has a session to mount the adapter to.
- if not request.session:+ if not hasattr(request, "session") or not request.session:
request.session = requests.Session()

API client name and version

google-genai==1.66.0

Reproduction steps: code

fromgoogleimportgenaifromgoogle.genaiimporttypesclient=genai.Client(
vertexai=True,
project='...",
location='global',
)
chat=client.chats.create(model='gemini-3-pro-preview')
response=chat.send_message("Hello. What is 2+2?")

Python environment

python 3.14

Python dependencies

[project]
name = "..."
version = "0.1.0"
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
"google-genai==1.66.0",
"ipykernel>=7.2.0",
"jupyter>=1.1.1",
]

Additional context

Error Log:

Details
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/chats.py:252, inChat.send_message(self, message, config)
247raiseValueError(
248f"Message must be a valid part type: {types.PartUnion} or"249f" {types.PartUnionDict}, got {type(message)}"250 )
251input_content=t.t_content(message)
--> [252](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/chats.py:252) response=self._modules.generate_content(
253model=self._model,
254contents=self._curated_history+ [input_content], # type: ignore[arg-type]255config=configifconfigelseself._config,
256 )
257model_output= (
258 [response.candidates[0].content]
259ifresponse.candidatesandresponse.candidates[0].content260else []
261 )
262automatic_function_calling_history= (
263response.automatic_function_calling_history264ifresponse.automatic_function_calling_history265else []
266 )
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/models.py:[5644](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/models.py:5644), inModels.generate_content(self, model, contents, config)
5642whileremaining_remote_calls_afc>0:
5643i+=1->5644response=self._generate_content(
5645model=model, contents=contents, config=parsed_config5646 )
5648function_map=_extra_utils.get_function_map(parsed_config)
5649ifnotfunction_map:
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/models.py:4306, inModels._generate_content(self, model, contents, config)
4303request_dict=_common.convert_to_dict(request_dict)
4304request_dict=_common.encode_unserializable_types(request_dict)
-> [4306](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/models.py:4306) response=self._api_client.request(
4307'post', path, request_dict, http_options4308 )
4310ifconfigisnotNoneandgetattr(
4311config, 'should_return_http_response', None4312 ):
4313return_value=types.GenerateContentResponse(sdk_http_response=response)
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1401, inBaseApiClient.request(self, http_method, path, request_dict, http_options)
1391defrequest(
1392self,
1393http_method: str,
(...) 1396http_options: Optional[HttpOptionsOrDict] =None,
1397 ) ->SdkHttpResponse:
1398http_request=self._build_request(
1399http_method, path, request_dict, http_options1400 )
-> [1401](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1401) response=self._request(http_request, http_options, stream=False)
1402response_body= (
1403response.response_stream[0] ifresponse.response_streamelse''1404 )
1405returnSdkHttpResponse(headers=response.headers, body=response_body)
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1237, inBaseApiClient._request(self, http_request, http_options, stream)
1234retry=tenacity.Retrying(**retry_kwargs)
1235returnretry(self._request_once, http_request, stream) # type: ignore[no-any-return]-> [1237](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1237) returnself._retry(self._request_once, http_request, stream)
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:470, inRetrying.__call__(self, fn, *args, **kwargs)
468retry_state=RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
469whileTrue:
--> [470](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:470) do=self.iter(retry_state=retry_state)
471ifisinstance(do, DoAttempt):
472try:
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:371, inBaseRetrying.iter(self, retry_state)
369result=None370foractioninself.iter_state.actions:
--> [371](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:371) result=action(retry_state)
372returnresultFile~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:413, inBaseRetrying._post_stop_check_actions.<locals>.exc_check(rs)
411retry_exc=self.retry_error_cls(fut)
412ifself.reraise:
--> [413](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:413) raiseretry_exc.reraise()
414raiseretry_excfromfut.exception()
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:184, inRetryError.reraise(self)
182defreraise(self) ->t.NoReturn:
183ifself.last_attempt.failed:
--> [184](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:184) raiseself.last_attempt.result()
185raiseselfFile~/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/concurrent/futures/_base.py:443, inFuture.result(self, timeout)
441raiseCancelledError()
442elifself._state==FINISHED:
--> [443](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/concurrent/futures/_base.py:443) returnself.__get_result()
445self._condition.wait(timeout)
447ifself._statein [CANCELLED, CANCELLED_AND_NOTIFIED]:
File~/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/concurrent/futures/_base.py:395, inFuture.__get_result(self)
393ifself._exceptionisnotNone:
394try:
--> [395](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/concurrent/futures/_base.py:395) raiseself._exception396finally:
397# Break a reference cycle with the exception in self._exception398self=NoneFile~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:473, inRetrying.__call__(self, fn, *args, **kwargs)
471ifisinstance(do, DoAttempt):
472try:
--> [473](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/tenacity/__init__.py:473) result=fn(*args, **kwargs)
474exceptBaseException: # noqa: B902475retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type]File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1180, inBaseApiClient._request_once(self, http_request, stream)
1178# If using proj/location, fetch ADC1179ifself.vertexaiand (self.projectorself.location):
-> [1180](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1180) http_request.headers['Authorization'] =f'Bearer {self._access_token()}'1181ifself._credentialsandself._credentials.quota_project_id:
1182http_request.headers['x-goog-user-project'] = (
1183self._credentials.quota_project_id1184 )
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1008, inBaseApiClient._access_token(self)
1006withself._sync_auth_lock:
1007ifnotself._credentials:
-> [1008](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:1008) self._credentials, project=load_auth(project=self.project)
1009ifnotself.project:
1010self.project=projectFile~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:191, inload_auth(project)
185## Set GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES to false186## to disable bound token sharing. Tracking on187## https://github.com/googleapis/python-genai/issues/1956188os.environ['GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES'] = (
189'false'190 )
--> [191](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/genai/_api_client.py:191) credentials, loaded_project_id=google.auth.default( # type: ignore[no-untyped-call]192scopes=['https://www.googleapis.com/auth/cloud-platform'],
193 )
195ifnotproject:
196project=loaded_project_idFile~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/_default.py:718, indefault(scopes, request, quota_project_id, default_scopes)
706checkers= (
707# Avoid passing scopes here to prevent passing scopes to user credentials.708# with_scopes_if_required() below will ensure scopes/default scopes are
(...) 714lambda: _get_gce_credentials(request, quota_project_id=quota_project_id),
715 )
717forcheckerincheckers:
--> [718](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/_default.py:718) credentials, project_id=checker()
719ifcredentialsisnotNone:
720credentials=with_scopes_if_required(
721credentials, scopes, default_scopes=default_scopes722 )
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/_default.py:714, indefault.<locals>.<lambda>()
700fromgoogle.auth.credentialsimportCredentialsWithQuotaProject702explicit_project_id=os.environ.get(
703environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)
704 )
706checkers= (
707# Avoid passing scopes here to prevent passing scopes to user credentials.708# with_scopes_if_required() below will ensure scopes/default scopes are709# safely set on the returned credentials since requires_scopes will710# guard against setting scopes on user credentials.711lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
712lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),
713_get_gae_credentials,
--> [714](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/_default.py:714) lambda: _get_gce_credentials(request, quota_project_id=quota_project_id),
715 )
717forcheckerincheckers:
718credentials, project_id=checker()
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/_default.py:410, in_get_gce_credentials(request, quota_project_id)
407ifrequestisNone:
408request=google.auth.transport._http_client.Request()
--> [410](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/_default.py:410) if_metadata.is_on_gce(request=request):
411# Get the project ID.412try:
413project_id=_metadata.get_project_id(request=request)
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py:135, inis_on_gce(request)
132if_NO_GCE_CHECK:
133returnFalse--> [135](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py:135) ifping(request):
136returnTrue138ifos.name=="nt":
139# TODO: implement GCE residency detection on WindowsFile~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py:205, inping(request, timeout, retry_count)
192"""Checks to see if the metadata server is available. 193  194 Args: (...) 202 bool: True if the metadata server is reachable, False otherwise. 203 """204use_mtls=_mtls.should_use_mds_mtls()
--> [205](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py:205) _prepare_request_for_mds(request, use_mtls=use_mtls)
206# NOTE: The explicit ``timeout`` is a workaround. The underlying207# issue is that resolving an unknown host on some networks will take208# 20-30 seconds; making this timeout short fixes the issue, but209# could lead to false negatives in the event that we are on GCE, but210# the metadata resolution was particularly slow. The latter case is211# "unlikely".212headers=_METADATA_HEADERS.copy()
File~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py:180, in_prepare_request_for_mds(request, use_mtls)
177# Only modify the request if mTLS is enabled.178ifuse_mtls:
179# Ensure the request has a session to mount the adapter to.--> [180](https://vscode-remote+ssh-002dremote-002bgcp-002dwbi-002dsaksham-002dsdx-002dusc1-002d01.vscode-resource.vscode-cdn.net/home/ext_saksham_goyal_compassdigital/data-eng-des-projects/npc/~/data-eng-des-projects/npc/.venv/lib/python3.14/site-packages/google/auth/compute_engine/_metadata.py:180) ifnotrequest.session:
181request.session=requests.Session()
183adapter=_mtls.MdsMtlsAdapter()
AttributeError: 'Request'objecthasnoattribute'session'

Metadata

Metadata

Assignees

Labels

authpriority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions