LLM Endpoint hosted in Azure databricks Response 200 but body is locked

Im having this issue from a POC I created with ReactJS and Flask and hosted in azure, I was given an LLM endpoint that is hosted in azure databricks. The endpoint works fine when I try it at postman but when Im trying to call it in my app the response is 200 but the body is locked. I’ve never encountered this issue in my previous project and I cant see if there is any permission or restrictions options to see the body in azure. Below is a snippet of my code and the endpoint response.

I’ve omitted the endpoint paths but im sure that it is correct

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#Flask app.py
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}})
@app.route("my-endpoint", methods=["POST"])
def handle_invocations(endpoint_name):
try:
payload = request.json
base_url = "https:{my-url}.azuredatabricks.net"
target_url = f"{my-target URL}"
headers = {
"Content-Type": "application/json",
"Authorization": "{my token}"
}
response = requests.post(target_url, json=payload, headers=headers)
return jsonify(response.json()), response.status_code
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)
# Reactjs Homepage.jsx
const onCardClick = async (question) => {
const payload = {
messages: [
{
role: "user",
content: String(question),
},
],
};
try {
const response = await fetch({my-endpoint}, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": {my-token}
},
body: JSON.stringify(payload),
});
if (!response.ok) {
let errorDetails = null;
try {
errorDetails = await response.json();
} catch (jsonError) {
console.error("Failed to parse error response as JSON.");
}
console.error("HTTP Error:", {
status: response.status,
statusText: response.statusText,
details: errorDetails
});
throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`);
}
const result = await response.json();
setTextFieldContent(result.content || "No response content available.");
} catch (error) {
console.error("Error:", error);
setTextFieldContent("An error occurred while fetching the response.");
}
};
</code>
<code>#Flask app.py import requests from flask import Flask, request, jsonify from flask_cors import CORS app = Flask(__name__) CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}}) @app.route("my-endpoint", methods=["POST"]) def handle_invocations(endpoint_name): try: payload = request.json base_url = "https:{my-url}.azuredatabricks.net" target_url = f"{my-target URL}" headers = { "Content-Type": "application/json", "Authorization": "{my token}" } response = requests.post(target_url, json=payload, headers=headers) return jsonify(response.json()), response.status_code except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == "__main__": app.run(debug=True) # Reactjs Homepage.jsx const onCardClick = async (question) => { const payload = { messages: [ { role: "user", content: String(question), }, ], }; try { const response = await fetch({my-endpoint}, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": {my-token} }, body: JSON.stringify(payload), }); if (!response.ok) { let errorDetails = null; try { errorDetails = await response.json(); } catch (jsonError) { console.error("Failed to parse error response as JSON."); } console.error("HTTP Error:", { status: response.status, statusText: response.statusText, details: errorDetails }); throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`); } const result = await response.json(); setTextFieldContent(result.content || "No response content available."); } catch (error) { console.error("Error:", error); setTextFieldContent("An error occurred while fetching the response."); } }; </code>
#Flask app.py
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)

CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}})

@app.route("my-endpoint", methods=["POST"])
def handle_invocations(endpoint_name):
    try:
        payload = request.json
        base_url = "https:{my-url}.azuredatabricks.net"
    
        target_url = f"{my-target URL}"
        

        headers = {
        "Content-Type": "application/json",
        "Authorization": "{my token}"
        }
    
        response = requests.post(target_url, json=payload, headers=headers)
    
        return jsonify(response.json()), response.status_code
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(debug=True)

# Reactjs Homepage.jsx
const onCardClick = async (question) => {
    const payload = {
        messages: [
            {
                role: "user",
                content: String(question),
            },
        ],
    };

    try {
        const response = await fetch({my-endpoint}, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Authorization": {my-token}
            },
            
            body: JSON.stringify(payload),
        });

        if (!response.ok) {
          let errorDetails = null;
  
          
          try {
              errorDetails = await response.json();
          } catch (jsonError) {
              console.error("Failed to parse error response as JSON.");
          }
  
          console.error("HTTP Error:", {
              status: response.status,
              statusText: response.statusText,
              details: errorDetails
          });
  
          throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`);
      }

        const result = await response.json();

        
        setTextFieldContent(result.content || "No response content available.");
    } catch (error) {
        console.error("Error:", error);
        setTextFieldContent("An error occurred while fetching the response.");
    }
};

2

The response what you are getting is correct and valid, access the content like below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const content = result.choices[0].message.content || "No response content available.";
</code>
<code>const content = result.choices[0].message.content || "No response content available."; </code>
const  content = result.choices[0].message.content || "No response content available.";

The response json data from endpoint will be something like below based on the arguments and body you pass, check the endpoint or LLM model documentation whichever you are using.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"id": "chatcmpl_cec8b3cd-856d-417b-a629-907e15a55762",
"object": "chat.completion",
"created": 1734327739,
"model": "dbrx-instruct-071224",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Databricks is a data and analytics company that provides a unified analytics platform for data science teams to collaborate on large-scale data engineering, data science, and machine learning workloads. The company was founded by the original creators of Apache Spark, Delta Lake, and MLflow. Databricks provides a managed platform for these open-source technologies, as well as additional proprietary features, to help organizations simplify and scale their data-driven initiatives. The platform enables data teams to process and analyze large volumes of data, build and deploy machine learning models, and share insights across the organization."
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 251,
"completion_tokens": 115,
"total_tokens": 366
}
}
</code>
<code>{ "id": "chatcmpl_cec8b3cd-856d-417b-a629-907e15a55762", "object": "chat.completion", "created": 1734327739, "model": "dbrx-instruct-071224", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Databricks is a data and analytics company that provides a unified analytics platform for data science teams to collaborate on large-scale data engineering, data science, and machine learning workloads. The company was founded by the original creators of Apache Spark, Delta Lake, and MLflow. Databricks provides a managed platform for these open-source technologies, as well as additional proprietary features, to help organizations simplify and scale their data-driven initiatives. The platform enables data teams to process and analyze large volumes of data, build and deploy machine learning models, and share insights across the organization." }, "finish_reason": "stop", "logprobs": null } ], "usage": { "prompt_tokens": 251, "completion_tokens": 115, "total_tokens": 366 } } </code>
{
  "id": "chatcmpl_cec8b3cd-856d-417b-a629-907e15a55762",
  "object": "chat.completion",
  "created": 1734327739,
  "model": "dbrx-instruct-071224",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Databricks is a data and analytics company that provides a unified analytics platform for data science teams to collaborate on large-scale data engineering, data science, and machine learning workloads. The company was founded by the original creators of Apache Spark, Delta Lake, and MLflow. Databricks provides a managed platform for these open-source technologies, as well as additional proprietary features, to help organizations simplify and scale their data-driven initiatives. The platform enables data teams to process and analyze large volumes of data, build and deploy machine learning models, and share insights across the organization."
      },
      "finish_reason": "stop",
      "logprobs": null
    }
  ],
  "usage": {
    "prompt_tokens": 251,
    "completion_tokens": 115,
    "total_tokens": 366
  }
}

Next, the body is locked because, read only once.

I got the below error when i tried.

code snippet

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>console.log(response)
const bd = await response.body.getReader().read();
console.log(bd)
const result = await response.json();
const content = result.choices[0].message.content || "No response content available.";
</code>
<code>console.log(response) const bd = await response.body.getReader().read(); console.log(bd) const result = await response.json(); const content = result.choices[0].message.content || "No response content available."; </code>
console.log(response)
const bd = await response.body.getReader().read();
console.log(bd)
const result = await response.json();
const content = result.choices[0].message.content || "No response content available.";

Error

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>TypeError: Failed to execute 'json' on 'Response': body stream already read
</code>
<code>TypeError: Failed to execute 'json' on 'Response': body stream already read </code>
TypeError: Failed to execute 'json' on 'Response': body stream already read

you can see in below screenshot, the values fetched for reading body using reader object and failed for .json() function.

Only once you can read for locked body, so read it as json and store in the variable.

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật