crewAIInc/crewAI · error · ValueError

Error fetching OAuth2 configuration: {e!s}

Error message

Error fetching OAuth2 configuration: {e!s}

What it means

A catch-all ValueError raised by the enterprise OAuth2 discovery flow when any exception other than httpx.HTTPError or the handled JSON decode path escapes — for example a failure inside _validate_oauth_config, a KeyError on response shape, or an unexpected bug in response handling. The original exception is chained (`from e`) so the cause is preserved, but the message only says 'Error fetching OAuth2 configuration: <str(e)>'.

Source

Thrown at lib/cli/src/crewai_cli/enterprise/main.py:68

            response = httpx.get(oauth_endpoint, timeout=30, headers=headers)
            response.raise_for_status()

            try:
                oauth_config = response.json()
            except json.JSONDecodeError as e:
                raise ValueError(f"Invalid JSON response from {oauth_endpoint}") from e

            self._validate_oauth_config(oauth_config)

            console.print(
                "✅ Successfully retrieved OAuth2 configuration", style="green"
            )
            return cast(dict[str, Any], oauth_config)

        except httpx.HTTPError as e:
            raise ValueError(f"Failed to connect to enterprise URL: {e!s}") from e
        except Exception as e:
            raise ValueError(f"Error fetching OAuth2 configuration: {e!s}") from e

    def _update_oauth_settings(
        self, enterprise_url: str, oauth_config: dict[str, Any]
    ) -> None:
        try:
            config_mapping = {
                "enterprise_base_url": enterprise_url,
                "oauth2_provider": oauth_config["provider"],
                "oauth2_audience": oauth_config["audience"],
                "oauth2_client_id": oauth_config["device_authorization_client_id"],
                "oauth2_domain": oauth_config["domain"],
                "oauth2_extra": oauth_config["extra"],
            }

            console.print("🔄 Updating local OAuth2 configuration...")

            for key, value in config_mapping.items():
                self.settings_command.set(key, value)

View on GitHub (pinned to 754d7323be)

Solutions

  1. Read the chained cause: run with the original exception visible (e.g. python -c or check traceback) to see what str(e) actually was
  2. curl the discovery endpoint and check the JSON is an object containing audience, domain, provider, device_authorization_client_id, extra
  3. Update the CLI (uv tool upgrade crewai) in case of schema skew with a newer enterprise server
  4. Report to the enterprise admin if the server emits an unexpected discovery document shape
Defensive patterns

Strategy: try-catch

Validate before calling

import httpx

def discovery_shape_ok(url: str) -> bool:
    r = httpx.get(url, timeout=30)
    r.raise_for_status()
    doc = r.json()
    return isinstance(doc, dict) and "provider" in doc

Type guard

def is_discovery_doc(value: object) -> bool:
    return isinstance(value, dict) and isinstance(value.get("provider"), str)

Try / catch

try:
    enterprise_cmd.connect(url)
except ValueError as e:
    cause = e.__cause__
    # inspect cause for the real failure (KeyError, TypeError, ...)
    log.warning("connect failed: %s (cause: %r)", e, cause)

Prevention

When it happens

Trigger: `crewai enterprise connect <url>` succeeding at HTTP level but failing afterwards: _validate_oauth_config raising (missing fields — which produces its own message, but validation KeyErrors like oauth_config['provider'] on a non-dict JSON do not), a JSON body that is a list/string instead of an object, or any unexpected type error while processing the response.

Common situations: Enterprise server returning well-formed JSON of the wrong shape (array, or object missing 'provider'), partial deployments where the discovery endpoint exists but the config backend is not fully configured, or version skew between CLI and server discovery schema.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/092a008a993cf18d. Report an issue: GitHub.