iflytek/astron-agent · error · CustomException

OPEN_AI_REQUEST_ERROR

OPEN_AI_REQUEST_ERROR

Error message

Anthropic request failed

What it means

This error is raised inside _normalize_event when a streamed Anthropic SDK event is an error-type event (event.type contains 'error'). The SDK surfaced a server-side error event mid-stream (e.g. an 'error' event in the SSE stream such as overloaded_error or invalid_request_error), and this adapter converts it into a CustomException with code OPEN_AI_REQUEST_ERROR. The message is taken from the event's error attribute, defaulting to 'Anthropic request failed' when the attribute is absent.

Solutions

  1. Read cause_error in the CustomException to see the exact Anthropic error event (type and message); fix the request parameter it names (model id, max_tokens, malformed message).
  2. If the event is overloaded_error, retry with exponential backoff — it is a transient Anthropic capacity issue, not a client bug.
  3. Verify the model name and parameter values against the Anthropic Messages API for the SDK version in use.
  4. If using a custom base_url (proxy/gateway), confirm the gateway forwards valid Anthropic SSE events and not its own error payloads.

Example fix

// before
response = client.messages.create(model="claude-3-5-sonnet-latest", max_tokens=200000, ...)
// after (valid model + within context limit)
response = client.messages.create(model="claude-3-5-sonnet-20241022", max_tokens=8192, ...)
Defensive patterns

Strategy: try-catch

Validate before calling

def validate_request(payload: dict) -> None:
    assert payload.get("model"), "model is required"
    assert isinstance(payload.get("max_tokens", 0), int) and payload["max_tokens"] > 0, "max_tokens must be a positive int"
    for m in payload.get("messages", []):
        assert m.get("role") in ("user", "assistant"), f"invalid role: {m.get('role')}"
validate_request(payload)

Type guard

def is_error_event(event) -> bool:
    t = getattr(event, "type", "") or ""
    return isinstance(t, str) and "error" in t

Try / catch

try:
    async for resp in llm.achat(messages):
        yield resp
except CustomException as e:
    if "overloaded" in str(e.cause_error).lower():
        await asyncio.sleep(backoff); retry()
    else:
        logger.error("Anthropic stream error: %s", e.cause_error)
        raise

Prevention

When it happens

Trigger: The Anthropic messages stream emits an event whose type contains 'error' during _recv_messages consumption — e.g. the API returns an overloaded_error (529), an invalid_request_error mid-stream, or the stream is aborted after starting. It is raised from the event-normalization loop, not from the initial HTTP handshake.

Common situations: Anthropic service overload/529s during streaming; sending a parameter the API rejects only after the stream opens (model name typo, too-large max_tokens); proxy/gateway (e.g. a base_url pointed at a compatible endpoint) returning error payloads as stream events; request aborted by content filters or long-running stream termination.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/63472912c0adf776. Report an issue: GitHub.

Appendix: source

Thrown at core/workflow/infra/providers/llm/anthropic/anthropic_chat_llm.py:246

                    "total_tokens": input_tokens + output_tokens,
                },
            }

        # Message stop event - signals end of stream
        elif isinstance(event, RawMessageStopEvent):
            return {
                "choices": [
                    {
                        "delta": {"content": "", "reasoning_content": ""},
                        "finish_reason": ChatStatus.FINISH_REASON.value,
                    }
                ],
                "usage": usage,
            }

        # Error event - raise exception
        elif hasattr(event, "type") and "error" in event.type:
            raise CustomException(
                err_code=CodeEnum.OPEN_AI_REQUEST_ERROR,
                err_msg=str(getattr(event, "error", "Anthropic request failed")),
                cause_error=str(event),
            )
        else:
            # Other event types we don't handle
            return None

    async def _recv_messages(  # noqa: C901
        self,
        url: str,
        user_message: list,
        extra_params: dict,
        span: Span,
        timeout: float | None = None,
    ) -> AsyncIterator[LLMResponse]:
        """
        Receive messages using Anthropic SDK streaming.

View on GitHub (pinned to 5e758547a8)