Home / Function/ parse_url_with_auth() — langchain Function Reference

parse_url_with_auth() — langchain Function Reference

Architecture documentation for the parse_url_with_auth() function in _utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  3350c254_f231_c0a4_315b_8d65c2892004["parse_url_with_auth()"]
  7fcdbe49_5c37_eba6_43b2_5192d6b4f79d["_utils.py"]
  3350c254_f231_c0a4_315b_8d65c2892004 -->|defined in| 7fcdbe49_5c37_eba6_43b2_5192d6b4f79d
  style 3350c254_f231_c0a4_315b_8d65c2892004 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/ollama/langchain_ollama/_utils.py lines 50–98

def parse_url_with_auth(
    url: str | None,
) -> tuple[str | None, dict[str, str] | None]:
    """Parse URL and extract `userinfo` credentials for headers.

    Handles URLs of the form: `https://user:password@host:port/path`

    Args:
        url: The URL to parse.

    Returns:
        A tuple of `(cleaned_url, headers_dict)` where:
        - `cleaned_url` is the URL without authentication credentials if any were
            found. Otherwise, returns the original URL.
        - `headers_dict` contains Authorization header if credentials were found.
    """
    if not url:
        return None, None

    parsed = urlparse(url)
    if not parsed.scheme or not parsed.netloc or not parsed.hostname:
        return None, None
    if not parsed.username:
        return url, None

    # Handle case where password might be empty string or None
    password = parsed.password or ""

    # Create basic auth header (decode percent-encoding)
    username = unquote(parsed.username)
    password = unquote(password)
    credentials = f"{username}:{password}"
    encoded_credentials = base64.b64encode(credentials.encode()).decode()
    headers = {"Authorization": f"Basic {encoded_credentials}"}

    # Strip credentials from URL
    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, headers

Domain

Subdomains

Frequently Asked Questions

What does parse_url_with_auth() do?
parse_url_with_auth() is a function in the langchain codebase, defined in libs/partners/ollama/langchain_ollama/_utils.py.
Where is parse_url_with_auth() defined?
parse_url_with_auth() is defined in libs/partners/ollama/langchain_ollama/_utils.py at line 50.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free