File size: 906 Bytes
395651c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Normalize URLs / env strings (HF secrets and copy-paste often include trailing newlines)."""


def sanitize_url(value: str | None) -> str | None:
    if value is None:
        return None
    s = value.strip().replace("\r", "").replace("\n", "").replace("\t", "")
    return s or None


def sanitize_env(value: str | None) -> str | None:
    """Strip whitespace and line breaks from environment-backed strings."""
    return sanitize_url(value)


# OpenAI SDK (>=1.x) requires a non-empty api_key at client construction (Docker build / prewarm has no secrets).
_OPENAI_API_KEY_BUILD_PLACEHOLDER = "build-placeholder-openrouter-not-for-production"


def openai_compatible_api_key(raw: str | None) -> str:
    """Return sanitized API key, or a placeholder so AsyncOpenAI() can be constructed without env at build time."""
    k = sanitize_env(raw)
    return k if k else _OPENAI_API_KEY_BUILD_PLACEHOLDER