diff --git a/ollama/_client.py b/ollama/_client.py index 18cb0fb4..8380b046 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -1404,7 +1404,7 @@ def _parse_host(host: Optional[str]) -> str: 'http://[0001:002:003:0004::1]:56789/path' """ - host, port = host or '', 11434 + host, port = (host or '').strip(), 11434 scheme, _, hostport = host.partition('://') if not hostport: scheme, hostport = 'http', host diff --git a/tests/test_client.py b/tests/test_client.py index 34657513..bd0d9a94 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -12,7 +12,7 @@ from pytest_httpserver import HTTPServer, URIPattern from werkzeug.wrappers import Request, Response -from ollama._client import CONNECTION_ERROR_MESSAGE, AsyncClient, Client, _copy_tools +from ollama._client import CONNECTION_ERROR_MESSAGE, AsyncClient, Client, _copy_tools, _parse_host from ollama._types import Image, Message PNG_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC' @@ -1324,6 +1324,12 @@ def test_tool_validation(): assert tools[0].function.name == 'test' +def test_parse_host_strips_whitespace(): + assert _parse_host(' http://example.com ') == 'http://example.com:80' + assert _parse_host(' example.com:56789 ') == 'http://example.com:56789' + assert _parse_host('\thttps://example.com/path\n') == 'https://example.com:443/path' + + def test_client_connection_error(): client = Client('http://localhost:1234')