openai-cookbook
openai-cookbook copied to clipboard
Trying the File Q&A
I keep getting Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client After uploading a file I tried both local and on vercel
I am getting the same error - did you resolve this?
Same here. This was my error:

This was the errors:
Warning: TT: undefined function: 32
Error: Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1540:34)
at TLSSocket.emit (node:events:513:28)
at TLSSocket._finishInit (node:_tls_wrap:959:8)
at ssl.onhandshakedone (node:_tls_wrap:743:12) {
code: 'SELF_SIGNED_CERT_IN_CHAIN',
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'User-Agent': 'OpenAI/NodeJS/3.1.0',
Authorization: 'Bearer sk-',
'Content-Length': 6860
},
method: 'post',
data: `{"model":"text-embedding-ada-002","input":["..."]}`,
url: 'https://api.openai.com/v1/embeddings'
},
request: <ref *1> Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
constructed: true,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false,
closeEmitted: false,
[Symbol(kOnFinished)]: []
},
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError],
socket: [Function: handleRequestSocket]
},
_eventsCount: 3,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'https:',
path: '/v1/embeddings',
method: 'POST',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'api.openai.com',
port: null,
nativeProtocols: [Object],
pathname: '/v1/embeddings'
},
_ended: false,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 6860,
_requestBodyBuffers: [ [Object] ],
_onNativeResponse: [Function (anonymous)],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
strictContentLength: false,
_contentLength: 6860,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: true,
_closed: false,
socket: [TLSSocket],
_header: 'POST /v1/embeddings HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'User-Agent: OpenAI/NodeJS/3.1.0\r\n' +
'Authorization: Bearer sk-\r\n' +
'Content-Length: 6860\r\n' +
'Host: api.openai.com\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: nop],
agent: [Agent],
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
joinDuplicateHeaders: undefined,
path: '/v1/embeddings',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'api.openai.com',
protocol: 'https:',
_redirectable: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(kBytesWritten)]: 0,
[Symbol(kEndCalled)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype],
[Symbol(errored)]: null,
[Symbol(kUniqueHeaders)]: null
},
_currentUrl: 'https://api.openai.com/v1/embeddings',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
wait - compiling /api/search-file-chunks (client and server)...
event - compiled successfully in 218 ms (61 modules)
Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1540:34)
at TLSSocket.emit (node:events:513:28)
at TLSSocket._finishInit (node:_tls_wrap:959:8)
at ssl.onhandshakedone (node:_tls_wrap:743:12) {
code: 'SELF_SIGNED_CERT_IN_CHAIN',
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'User-Agent': 'OpenAI/NodeJS/3.1.0',
Authorization: 'Bearer sk-',
'Content-Length': 88
},
method: 'post',
data: '{"model":"text-embedding-ada-002","input":"What are the minimum disclosure agreements?"}',
url: 'https://api.openai.com/v1/embeddings'
},
request: <ref *1> Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
constructed: true,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false,
closeEmitted: false,
[Symbol(kOnFinished)]: []
},
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError],
socket: [Function: handleRequestSocket]
},
_eventsCount: 3,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'https:',
path: '/v1/embeddings',
method: 'POST',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'api.openai.com',
port: null,
nativeProtocols: [Object],
pathname: '/v1/embeddings'
},
_ended: false,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 88,
_requestBodyBuffers: [ [Object] ],
_onNativeResponse: [Function (anonymous)],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
strictContentLength: false,
_contentLength: 88,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: true,
_closed: false,
socket: [TLSSocket],
_header: 'POST /v1/embeddings HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'User-Agent: OpenAI/NodeJS/3.1.0\r\n' +
'Authorization: Bearer sk-\r\n' +
'Content-Length: 88\r\n' +
'Host: api.openai.com\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: nop],
agent: [Agent],
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
joinDuplicateHeaders: undefined,
path: '/v1/embeddings',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'api.openai.com',
protocol: 'https:',
_redirectable: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(kBytesWritten)]: 0,
[Symbol(kEndCalled)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype],
[Symbol(errored)]: null,
[Symbol(kUniqueHeaders)]: null
},
_currentUrl: 'https://api.openai.com/v1/embeddings',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
wait - compiling /api/get-answer-from-files (client and server)...
event - compiled successfully in 183 ms (62 modules)
Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1540:34)
at TLSSocket.emit (node:events:513:28)
at TLSSocket._finishInit (node:_tls_wrap:959:8)
at ssl.onhandshakedone (node:_tls_wrap:743:12) {
code: 'SELF_SIGNED_CERT_IN_CHAIN',
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'User-Agent': 'OpenAI/NodeJS/3.1.0',
Authorization: 'Bearer sk-',
'Content-Length': 1198
},
method: 'post',
responseType: 'stream',
data: `{"prompt":"Given a question, try to answer it using the content of the file extracts below, and if you cannot answer, or find a relevant file, just output \\"I couldn't find the answer to that question in your files.\\".\\n\\nIf the answer is not contained in the files or if there are no file extracts, respond with \\"I couldn't find the answer to that question in your files.\\" If the question is not actually a question, respond with \\"That's not a valid question.\\"\\n\\nIn the cases where you can find the answer, first give the answer. Then explain how you found the answer from the source or sources, and use the exact filenames of the source files you mention. Do not make up the names of any other files other than those mentioned in the files context. Give the answer in markdown format.Use the following format:\\n\\nQuestion: <question>\\n\\nFiles:\\n<###\\n\\"filename 1\\"\\nfile text>\\n<###\\n\\"filename 2\\"\\nfile text>...\\n\\nAnswer: <answer or \\"I couldn't find the answer to that question in your files\\" or \\"That's not a valid question.\\">\\n\\nQuestion: What are the minimum disclosure agreements?\\n\\nFiles:\\n\\n\\nAnswer:","max_tokens":800,"temperature":0,"model":"text-davinci-003","stream":true}`,
url: 'https://api.openai.com/v1/completions'
},
request: <ref *1> Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
constructed: true,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false,
closeEmitted: false,
[Symbol(kOnFinished)]: []
},
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError],
socket: [Function: handleRequestSocket]
},
_eventsCount: 3,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'https:',
path: '/v1/completions',
method: 'POST',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'api.openai.com',
port: null,
nativeProtocols: [Object],
pathname: '/v1/completions'
},
_ended: false,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 1198,
_requestBodyBuffers: [ [Object] ],
_onNativeResponse: [Function (anonymous)],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
strictContentLength: false,
_contentLength: 1198,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: true,
_closed: false,
socket: [TLSSocket],
_header: 'POST /v1/completions HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'User-Agent: OpenAI/NodeJS/3.1.0\r\n' +
'Authorization: Bearer sk-\r\n' +
'Content-Length: 1198\r\n' +
'Host: api.openai.com\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: nop],
agent: [Agent],
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
joinDuplicateHeaders: undefined,
path: '/v1/completions',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'api.openai.com',
protocol: 'https:',
_redirectable: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(kBytesWritten)]: 0,
[Symbol(kEndCalled)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype],
[Symbol(errored)]: null,
[Symbol(kUniqueHeaders)]: null
},
_currentUrl: 'https://api.openai.com/v1/completions',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
error - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:399:5)
at ServerResponse.setHeader (node:_http_outgoing:663:11)
at _res.setHeader (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\base-server.js:129:24)
at sendJson (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\api-utils\node.js:195:9)
at apiRes.json (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\api-utils\node.js:351:31)
at handler (webpack-internal:///(api)/./src/pages/api/get-answer-from-files.ts:52:25)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.apiResolver (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\api-utils\node.js:363:9)
at async DevServer.runApi (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\next-server.js:488:9)
at async Object.fn (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\next-server.js:750:37)
at async Router.execute (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\router.js:253:36)
at async DevServer.run (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\base-server.js:384:29)
at async DevServer.run (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\dev\next-dev-server.js:741:20)
at async DevServer.handleRequest (C:\Users\gpteohxh\Desktop\tutorial\openai-cookbook-main\apps\file-q-and-a\nextjs\node_modules\next\dist\server\base-server.js:322:20) {
code: 'ERR_HTTP_HEADERS_SENT',
page: '/api/get-answer-from-files'
}
I was getting this error initially but after restarting the server it went away 🤷♂️
I was getting this error initially but after restarting the server it went away 🤷♂️
How do you restart the server?
@xuhaoteoh I was using the nextjs version running locally. Just Ctrl-C and running npm run dev again.
@kutaibah did you find a solution for this? It was working fine for me in the morning but all of sudden this error start flowing. Any idea or solution you received?
Same here. This was my error:
Does anyone get any answer of this issue? I'm facing the same and posted issue but no replies yet :
https://github.com/openai/openai-cookbook/issues/159
Seems like it’s a problem with SSL certification. When I use my personal laptop instead of my work laptop, the app works fine and there was no errors.
On Sun, 26 Feb 2023 at 2:44 PM, prateek @.***> wrote:
Same here. This was my error: [image: MicrosoftTeams-image (7)] https://user-images.githubusercontent.com/56465408/220263926-99b85dbf-4e18-44c1-9bc2-8eb614121a6a.png
Does anyone get any answer of this issue? I'm facing the same and posted issue but no replies yet :
#159 https://github.com/openai/openai-cookbook/issues/159
— Reply to this email directly, view it on GitHub https://github.com/openai/openai-cookbook/issues/135#issuecomment-1445281134, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANOZQAEHUDYMHFR52LSDKZDWZL3WLANCNFSM6AAAAAAU47WPQE . You are receiving this because you were mentioned.Message ID: @.***>
@xuhaoteoh SSL error from openAI side ? because I have uploaded the same to Vercel and Render for dynamic URL. it loads fine there but throws the same error. It was working for me day before yesterday and results were really good, All of sudden it stopped working.
I didn't find the solution it is not ssl for sure,
@kutaibah @ted-at-openai it's working. The reason behind it will be too many requests from same API key. Everything works for me now. Just create a new key and make sure you won't upload this key to Github or Gitlab as public repository, as soon as you upload it Your OpenAI API key was determined to have been leaked, which has triggered a key rotation.
If you have one more laptop or pc try same code with new key there it will work.
Having the same issue. error - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Tried restarting the server and changing the API key. Still no luck.
I'm closing the issue. Solution: I have created another account or in the same account created a new API key, I believe it's related to too many requests from the same API key which is causing the issue. with a fresh key and fresh account, it works with the Local environment as well as uploaded to vercel or render.
Following @patriksharma 's advice in https://github.com/openai/openai-cookbook/issues/158#issuecomment-1445575476, creating a new account solved the issue. Creating new API key with my first account did not work. Not sure why. Thanks.
Found out the reason why any API key from my first account did not work. Was because it was past the 3 month trial period for that account. Make sure to check your account is still on a plan (whether free or paid). You can check that here: https://platform.openai.com/account/usage
Does anyone have a solution?If so please let me know. ToT

@Cloud-Paris see my comments above (https://github.com/openai/openai-cookbook/issues/135#issuecomment-1492961202 and https://github.com/openai/openai-cookbook/issues/135#issuecomment-1496908414). Have you checked if your API key is not expired?
I have figured out that this is due to your free trial period (first 3 months of account opening) running out. You will have to upgrade your membership to get access to the api.