Portkey-AI / gateway

A Blazing Fast AI Gateway with integrated Guardrails. Route to 200+ LLMs, 50+ AI Guardrails with 1 fast & friendly API.
https://portkey.ai/features/ai-gateway
MIT License
5.85k stars 403 forks source link

`"Content-Length can't be present with Transfer-Encoding` #474

Open davidfant opened 1 month ago

davidfant commented 1 month ago

What Happened?

I'm running the gateway locally using npx @portkey-ai/gateway and using the NodeJS SDK I got weird connection errors

const portkey = new Portkey({
    apiKey: " ", // need to put something here otherwise portkey will complain
    baseURL: process.env.PORTKEY_GATEWAY_URL,
    Authorization: `Bearer ${process.env.OPENAI_API_KEY!}`,
    provider: "openai",
  });

  const completion = await portkey.chat.completions.create({
    messages: [{ role: "user", content: "Hello, world! 1" }],
    model: "gpt-3.5-turbo",
    temperature: 0,
  });

The following CURL request works:

curl http://localhost:8787/v1/chat/completions -H "x-portkey-provider: openai"  -H "Content-Type: application/json"   -H "Authorization: ..."   -d '{
    "model": "gpt-3.5-turbo",
    "messages": [
      {
        "role": "system",
        "content": "Hello world"
      }
    ]
  }' | jq

But in node with either fetch, node-fetch or axios I'm always getting the error "Content-Length can't be present with Transfer-Encoding" from Portkey

axios
      .post("http://localhost:8787/v1/chat/completions", data, {
        headers: {
          "x-portkey-provider": "openai",
          "Content-Type": "application/json",
          Authorization: "Bearer " + process.env.OPENAI_API_KEY!,
        },
      })
      .then((response) => {
        console.log(response.data);
      })
      .catch((error) => {
        console.error("Error:", error);
      })

This works as expected when running using Docker

What Should Have Happened?

The Node SDK should work

Relevant Code Snippet

No response

Your Twitter/LinkedIn

https://twitter.com/da_fant

narengogi commented 1 month ago

Hey @davidfant can you add the stack trace? Are you sure this is not a client error? I've run the code snippet in my local without any exceptions

VisargD commented 1 month ago

Hey! Can you please make sure that you are using the latest version of the gateway.

gabrielmontagne commented 1 month ago

Having the same problem with @portkey-ai/gateway@1.5.7 and the node SDK "portkey-ai": "^1.3.2" on the node client.

APIConnectionError: Connection error.
    at OpenAI.makeRequest (file:///tmp/ttaa/node_modules/openai/core.mjs:297:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (file:///tmp/ttaa/index.mjs:15:26) {
  status: undefined,
  headers: undefined,
  request_id: undefined,
  error: undefined,
  code: undefined,
  param: undefined,
  type: undefined,
  cause: FetchError: request to http://127.0.0.1:8787/v1/chat/completions failed, reason: Parse Error: Content-Length can't be present with Transfer-Encoding
      at ClientRequest.<anonymous> (/tmp/ttaa/node_modules/node-fetch/lib/index.js:1501:11)
      at ClientRequest.emit (node:events:513:28)
      at Socket.socketOnData (node:_http_client:551:9)
      at Socket.emit (node:events:513:28)
      at addChunk (node:internal/streams/readable:324:12)
      at readableAddChunk (node:internal/streams/readable:297:9)
      at Readable.push (node:internal/streams/readable:234:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
    type: 'system',
    errno: 'HPE_UNEXPECTED_CONTENT_LENGTH',
    code: 'HPE_UNEXPECTED_CONTENT_LENGTH'
  }
}
VisargD commented 1 month ago

Hey! We have identified the cause for this. Will be pushing a patch soon

gabrielmontagne commented 2 weeks ago

I wonder if you have an ETA? We've parked our integration with Portkey pending on this fix.