The error you're encountering, "Content-Length can't be present with Transfer-Encoding," suggests that the response from your proxy server inadvertently includes both Content-Length and Transfer-Encoding headers, which is not allowed by HTTP/1.1 specifications. This can happen if the original response from the AI provider’s API contains a Transfer-Encoding: chunked header and your proxy server script is also calculating and setting a Content-Length header implicitly or explicitly.
To resolve this issue, you should ensure that you do not forward both Content-Length and Transfer-Encoding headers from the AI provider's responses through your proxy. The simplest way to handle this is by removing the Content-Length header if Transfer-Encoding is present.
You can adjust the proxy functions to filter out the Content-Length header in such cases. Here’s an updated version of the proxy_request function that adds this logic:
from flask import Flask, request, Response
import requests
app = Flask(__name__)
AI_PROVIDER_BASE_URL = 'https://your.ai.provider/api'
PROXY_BASE_PATH = '/v1'
FORWARDED_BASE_URL = AI_PROVIDER_BASE_URL + '/v1'
@app.route(PROXY_BASE_PATH + '/models', methods=['GET', 'POST'])
def proxy_models():
response = requests.request(
method=request.method,
url=f'{AI_PROVIDER_BASE_URL}/models',
headers={key: value for (key, value) in request.headers if key != 'Host'},
data=request.get_data(),
cookies=request.cookies,
allow_redirects=False)
filtered_headers = {key: value for (key, value) in response.headers.items() if key != 'Content-Length' or 'Transfer-Encoding' not in response.headers}
return Response(response.content, status=response.status_code, headers=filtered_headers)
@app.route(PROXY_BASE_PATH + '/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy_request(path):
response = requests.request(
method=request.method,
url=f'{FORWARDED_BASE_URL}/{path}',
headers={key: value for (key, value) in request.headers if key != 'Host'},
data=request.get_data(),
cookies=request.cookies,
allow_redirects=False)
filtered_headers = {key: value for (key, value) in response.headers.items() if key != 'Content-Length' or 'Transfer-Encoding' not in response.headers}
return Response(response.content, status=response.status_code, headers=filtered_headers)
if __name__ == '__main__':
app.run(debug=True)
In this revised example, before returning the response in both proxy_models and proxy_request functions, headers from the AI provider's response are filtered through a condition that effectively removes the Content-Length header if Transfer-Encoding is present.
This adjustment ensures you aren't simultaneously sending Content-Length and Transfer-Encoding: chunked headers, adhering to the HTTP/1.1 specification and avoiding the error you encountered.
This solution aims to directly address the most common scenario leading to the mentioned error. Make sure to adjust other parts of your proxy server accordingly if your implementation requires handling headers or the responses more specifically.