Skip to content

Commit

Permalink
Shallow copy headers to avoid mutating the original (#30)
Browse files Browse the repository at this point in the history
With Codestral and maybe other backends the same headers are passed to
Get for every request. Without this change for each request an
additional Content-Type header is added. Codestral fails when multiple
Content-Type headers are present.

For example when using Codestral I generated the folloing curl args
```
{ "https://codestral.mistral.ai/v1/fim/completions", "-d", "@/tmp/lua_vZiQ6f", "-H", "Authorization: Bearer ************************", "-H", "Content-Type: application/json", "-H", "Content-Type: application/json", "-H", "Content-Type: application/json", "-H", "Content-Type: application/json" }
```

And I get the response:
```
{\n  code = vim.NIL,\n  message = "Input should be a valid dictionary or object to extract fields from",\n  object = "error",\n  param = vim.NIL,\n  type = "invalid_request_error"\n}
```

This commit makes a shallow copy of the headers before adding additional
headers
  • Loading branch information
mrloop authored Sep 15, 2024
1 parent 5bf7e0e commit a4d56ec
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lua/cmp_ai/requests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Service:json_decode(data)
end

function Service:Get(url, headers, data, cb)
headers = headers or {}
headers = vim.tbl_extend("force", {}, headers or {})
headers[#headers + 1] = 'Content-Type: application/json'

local tmpfname = os.tmpname()
Expand Down

0 comments on commit a4d56ec

Please sign in to comment.