BerriAI/litellm · error · ValueError

ref must be a non-empty string

Error message

ref must be a non-empty string

What it means

GitLabClient.set_ref replaces the default ref (tag or branch) used by all subsequent GitLab API calls; it rejects falsy arguments, so passing '' or None raises immediately. This is a local config-plumbing error raised before anything is sent over the network.

Solutions

  1. Only call set_ref when you actually have a non-empty ref
  2. Default the override to a concrete branch (e.g. 'main') instead of '' or None
  3. Normalize first: ref = (ref or '').strip(), then guard before calling

Example fix

# before
client.set_ref(git_ref)  # git_ref may be None or ""

# after
if git_ref and git_ref.strip():
    client.set_ref(git_ref.strip())
Defensive patterns

Strategy: validation

Validate before calling

def normalize_ref(ref: str | None) -> str | None:
    """Return a clean ref or None; call set_ref only when not None."""
    cleaned = (ref or "").strip()
    return cleaned or None


clean_ref = normalize_ref(git_ref)
if clean_ref:
    client.set_ref(clean_ref)

Type guard

def is_valid_ref(ref: object) -> bool:
    """True when ref is a non-empty, non-whitespace string."""
    return isinstance(ref, str) and bool(ref.strip())

Try / catch

try:
    client.set_ref(ref)
except ValueError as e:
    if "ref must be a non-empty string" in str(e):
        raise RuntimeError(f"invalid git ref configured: {ref!r}") from e
    raise

Prevention

When it happens

Trigger: set_ref(os.getenv('GITLAB_REF')) with the variable unset (None); set_ref('') from a config default; a computed ref like `tag or branch` that evaluated to an empty string; calling set_ref unconditionally for an 'optional' override.

Common situations: Optional per-prompt ref overrides wired from config that are usually absent; refs derived from release variables that are empty outside CI; mixing None-vs-'' conventions across config layers.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/a316aae38efe4588. Report an issue: GitHub.

Appendix: source

Thrown at litellm/integrations/gitlab/gitlab_client.py:108

        self,
        directory_path: str = "",
        recursive: bool = False,
        *,
        ref: str | None = None,
    ) -> str:
        path_q: Final = f"&path={quote(directory_path, safe='')}" if directory_path else ""
        rec_q: Final = "&recursive=true" if recursive else ""
        ref_q: Final = quote(ref or self.ref, safe="")
        return f"{self.base_url}/projects/{self._project_enc}/repository/tree?ref={ref_q}{path_q}{rec_q}"

    # ------------------------
    # Public API
    # ------------------------

    def set_ref(self, ref: str) -> None:
        """Override the default ref (tag/branch) for subsequent calls."""
        if not ref:
            raise ValueError("ref must be a non-empty string")
        self.ref = ref

    def get_file_content(self, file_path: str, *, ref: str | None = None) -> str | None:
        """
        Fetch the content of a file from the GitLab repository at the given ref
        (tag, branch, or commit SHA). If `ref` is None, uses self.ref.

        Strategy:
          1) Try the RAW endpoint (returns bytes of the file)
          2) Fallback to the JSON endpoint (returns base64-encoded content)

        Returns:
            File content as UTF-8 string, or None if file not found.
        """
        raw_url: Final = self._file_raw_url(file_path, ref=ref)

        try:
            resp: Final = self.http_handler.get(raw_url, headers=self.headers)

View on GitHub (pinned to 77b7c6c40c)