Skip to content

@effect/ai-anthropic: messageBatchesResults fails with "Unexpected status code" on its 200 JSON Lines response #10107

Closed

Description

@mannyc2

What happens

The generated client's messageBatchesResults fails with StatusCode: Unexpected status code on a successful response. GET /v1/messages/batches/{id}/results returns 200 with a JSON Lines body (application/x-jsonl), one result per line. The operation in src/Generated.ts matches only 4xx, and sends every other status to unexpectedStatus:

withResponse(options?.config)(HttpClientResponse.matchStatus({
  "4xx": decodeError("MessageBatchesResults4XX", MessageBatchesResults4XX),
  orElse: unexpectedStatus
}))

So a batch's results can't be read through the client at all.

Repro

With @effect/[email protected] and [email protected], and a client that answers the way the API does:

import { Effect } from "effect"
import * as HttpClient from "effect/http/HttpClient"
import * as HttpClientRequest from "effect/http/HttpClientRequest"
import * as HttpClientResponse from "effect/http/HttpClientResponse"
import * as Generated from "@effect/ai-anthropic/Generated"

const jsonl = [
  { custom_id: "a", result: { type: "succeeded", message: { id: "msg_1" } } },
  { custom_id: "b", result: { type: "errored", error: { type: "error", error: { type: "api_error", message: "x" } } } }
].map((line) => JSON.stringify(line)).join("\n")

const httpClient = HttpClient.make((request) =>
  Effect.succeed(HttpClientResponse.fromWeb(request, new Response(jsonl, {
    status: 200,
    headers: { "content-type": "application/x-jsonl" }
  })))
)

const client = Generated.make(httpClient.pipe(HttpClient.mapRequest(HttpClientRequest.prependUrl("https://api.anthropic.com"))))
console.log(String(await Effect.runPromiseExit(client.messageBatchesResults("msgbatch_1", { params: {} }))))
// Failure(Cause([Fail(HttpClientError: StatusCode: Unexpected status code (200 GET https://api.anthropic.com/v1/messages/batches/msgbatch_1/results))]))

We hit the same failure against the live API after a batch had ended.

Workaround

GET the batch's results_url with HttpClient and decode each line with a Schema of the result.

A possible fix

Give the operation a 2xx case that decodes the body line by line with a schema for one result (custom_id and result), which Generated.ts has none of yet, and returns them as an array or as a Stream, since a batch's results can be large.

Versions: 4.0.0 through 4.1.0, and main.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions