Skip to content

Conversation

@ambershen
Copy link

Fixes #33986.

Summary:

  • Normalize scheme-less base_url values (e.g., ollama:11434) by defaulting to http:// when the input resembles host:port.
  • Preserve and merge Authorization headers when userinfo credentials are present, both for sync and async clients.
  • Add unit tests covering scheme-less host:port and scheme-less userinfo credentials.

Implementation details:

  • Update parse_url_with_auth to accept scheme-less endpoints, producing a cleaned URL with explicit scheme and extracted auth headers.
  • No changes required in OllamaLLM, ChatOllama, or OllamaEmbeddings—they already consume the cleaned URL and headers.

Why:

  • Previously, scheme-less inputs caused parse_url_with_auth to return (None, None), leading Ollama clients to fall back to defaults and ignore the provided base_url.

Tests:

  • Extended libs/partners/ollama/tests/unit_tests/test_auth.py to cover the new cases.

Notes:

  • Default scheme chosen is http to match common Ollama local deployments. Users can still explicitly provide https:// when appropriate.

@github-actions github-actions bot added security integration Related to a provider partner package integration ollama labels Nov 19, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 19, 2025

CodSpeed Performance Report

Merging #34042 will not alter performance

Comparing ambershen:fix/ollama-base-url-ignored-33986 (804984f) with master (cbaea35)1

Summary

✅ 1 untouched
⏩ 33 skipped2

Footnotes

  1. No successful run was found on master (30e2260) during the generation of this report, so cbaea35 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 33 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@mdrxy mdrxy changed the title Respect scheme-less base_url for Ollama and add tests (#33986) fix(ollama): Respect scheme-less base_url Nov 24, 2025
@github-actions github-actions bot added the fix label Nov 24, 2025
@mdrxy mdrxy requested a review from Copilot November 24, 2025 00:56
Copilot finished reviewing on behalf of mdrxy November 24, 2025 00:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #33986 by adding support for scheme-less base_url values in the Ollama integration. The implementation normalizes scheme-less inputs (e.g., ollama:11434 or user:password@host:port) by defaulting to http:// scheme when appropriate.

Key changes:

  • Enhanced parse_url_with_auth() function to detect and handle scheme-less URLs
  • Added URL reconstruction logic to ensure consistent output format
  • Extended test coverage with two new test cases for scheme-less scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
libs/partners/ollama/langchain_ollama/_utils.py Modified parse_url_with_auth() to detect scheme-less URLs and add default http:// scheme, plus reconstruct URLs consistently
libs/partners/ollama/tests/unit_tests/test_auth.py Added tests for scheme-less host:port and scheme-less URLs with credentials

Comment on lines +80 to +90
cleaned_netloc = parsed.hostname or ""
if parsed.port:
cleaned_netloc += f":{parsed.port}"
cleaned_url = f"{parsed.scheme}://{cleaned_netloc}"
if parsed.path:
cleaned_url += parsed.path
if parsed.query:
cleaned_url += f"?{parsed.query}"
if parsed.fragment:
cleaned_url += f"#{parsed.fragment}"
return cleaned_url, None
Copy link

Copilot AI Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code duplication: The URL reconstruction logic (lines 80-90 and 103-113) is duplicated. Consider extracting this into a helper function to improve maintainability and reduce the risk of inconsistencies.

Example:

def _reconstruct_url(parsed: ParseResult) -> str:
    """Reconstruct URL from parsed components without userinfo."""
    cleaned_netloc = parsed.hostname or ""
    if parsed.port:
        cleaned_netloc += f":{parsed.port}"
    
    cleaned_url = f"{parsed.scheme}://{cleaned_netloc}"
    if parsed.path:
        cleaned_url += parsed.path
    if parsed.query:
        cleaned_url += f"?{parsed.query}"
    if parsed.fragment:
        cleaned_url += f"#{parsed.fragment}"
    return cleaned_url

Then use it in both places:

if not parsed.username:
    return _reconstruct_url(parsed), None
# ... later ...
return _reconstruct_url(parsed), headers

Copilot uses AI. Check for mistakes.
Comment on lines +70 to +78
if not (parsed.scheme in {"http", "https"} and parsed.netloc and parsed.hostname):
if ":" in url:
parsed_with_scheme = urlparse(f"http://{url}")
if parsed_with_scheme.netloc and parsed_with_scheme.hostname:
parsed = parsed_with_scheme
else:
return None, None
else:
return None, None
Copy link

Copilot AI Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect handling of non-HTTP schemes: The condition at line 70 will incorrectly attempt to add http:// prefix to URLs with valid schemes other than http or https (e.g., ftp://example.com:21).

When the scheme is not in {"http", "https"}, the code falls through to line 71 and tries to prepend http://, resulting in http://ftp://example.com:21, which is invalid.

Consider revising the logic to:

if not parsed.scheme:
    # No scheme provided - try adding http://
    if parsed.netloc or ":" in url:
        parsed_with_scheme = urlparse(f"http://{url}")
        if parsed_with_scheme.netloc and parsed_with_scheme.hostname:
            parsed = parsed_with_scheme
        else:
            return None, None
    else:
        return None, None
elif parsed.scheme not in {"http", "https"}:
    # Unsupported scheme
    return None, None
elif not (parsed.netloc and parsed.hostname):
    # Missing netloc or hostname
    return None, None

This approach:

  1. Checks for missing scheme separately
  2. Rejects unsupported schemes explicitly
  3. Validates netloc/hostname requirements

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix integration Related to a provider partner package integration ollama security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OllamaLLM base_url is ignored.

2 participants