fix: add missing authorization check in AsyncClient web_search and web_fetch

The sync Client.web_search and Client.web_fetch both validate that a
Bearer token is present in the authorization header before making
requests to ollama.com API endpoints. However, their async counterparts
in AsyncClient are missing this check entirely, allowing unauthenticated
requests to go through and fail with unclear server-side errors.

Add the same authorization header validation to AsyncClient.web_search
and AsyncClient.web_fetch to match the sync implementation and provide
clear error messages when credentials are missing.

Signed-off-by: JiangNan <1394485448@qq.com>
This commit is contained in:
JiangNan 2026-03-08 22:09:30 +08:00
parent dbccf192ac
commit 7a36db4abb

View File

@ -801,7 +801,12 @@ class AsyncClient(BaseClient):
Returns:
WebSearchResponse with the search results
Raises:
ValueError: If OLLAMA_API_KEY environment variable is not set
"""
if not self._client.headers.get('authorization', '').startswith('Bearer '):
raise ValueError('Authorization header with Bearer token is required for web search')
return await self._request(
WebSearchResponse,
'POST',
@ -821,7 +826,12 @@ class AsyncClient(BaseClient):
Returns:
WebFetchResponse with the fetched result
Raises:
ValueError: If OLLAMA_API_KEY environment variable is not set
"""
if not self._client.headers.get('authorization', '').startswith('Bearer '):
raise ValueError('Authorization header with Bearer token is required for web fetch')
return await self._request(
WebFetchResponse,
'POST',