Skip to content

Commit 2589662

Browse files
Adding headers to HttpRequestResponse when making http_request
1 parent 9548a18 commit 2589662

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

databricks/sdk/mixins/open_ai_client.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from requests import Response
55

66
from databricks.sdk.service.serving import (ExternalFunctionRequestHttpMethod,
7-
ServingEndpointsAPI)
8-
7+
ServingEndpointsAPI,
8+
HttpRequestResponse)
99

1010
class ServingEndpointsExt(ServingEndpointsAPI):
1111

@@ -88,15 +88,30 @@ def http_request(
8888
"""
8989
response = Response()
9090
response.status_code = 200
91-
server_response = super().http_request(
92-
connection_name=conn,
93-
method=method,
94-
path=path,
95-
headers=js.dumps(headers) if headers is not None else None,
96-
json=js.dumps(json) if json is not None else None,
97-
params=js.dumps(params) if params is not None else None,
91+
92+
# We currently don't call super.http_request because we need to pass in response_headers
93+
# This is a temporary fix to get the headers we need for the MCP session id
94+
# TODO: Remove this once we have a better way to get back the response headers
95+
headers_to_capture = ["mcp-session-id"]
96+
res = self._api.do(
97+
"POST",
98+
"/api/2.0/external-function",
99+
body={
100+
"connection_name": conn,
101+
"method": method.value,
102+
"path": path,
103+
"headers": js.dumps(headers) if headers is not None else None,
104+
"json": js.dumps(json) if json is not None else None,
105+
"params": js.dumps(params) if params is not None else None,
106+
},
107+
headers={"Accept": "text/plain", "Content-Type": "application/json"},
108+
raw=True,
109+
response_headers=headers_to_capture
98110
)
99111

112+
# Create HttpRequestResponse from the raw response
113+
server_response = HttpRequestResponse.from_dict(res)
114+
100115
# Read the content from the HttpRequestResponse object
101116
if hasattr(server_response, "contents") and hasattr(server_response.contents, "read"):
102117
raw_content = server_response.contents.read() # Read the bytes
@@ -109,4 +124,9 @@ def http_request(
109124
else:
110125
raise ValueError("Contents must be bytes.")
111126

127+
# Copy headers from raw response to Response
128+
for header_name in headers_to_capture:
129+
if header_name in res:
130+
response.headers[header_name] = res[header_name]
131+
112132
return response

tests/test_open_ai_mixin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_http_request(w, requests_mock):
3737
"Accept": "text/plain",
3838
"Content-Type": "application/json",
3939
}
40+
headers_to_capture = ["mcp-session-id"]
4041
mocked_url = "http://localhost/api/2.0/external-function"
4142
blob_response = BytesIO(b"The request was successful")
4243

@@ -45,6 +46,7 @@ def test_http_request(w, requests_mock):
4546
request_headers=headers,
4647
content=blob_response.getvalue(),
4748
status_code=200,
49+
response_headers=headers_to_capture,
4850
)
4951
response = w.serving_endpoints.http_request(
5052
conn="test_conn",
@@ -55,3 +57,4 @@ def test_http_request(w, requests_mock):
5557
assert requests_mock.called
5658
assert response.status_code == 200 # Verify the response status
5759
assert response.text == "The request was successful" # Ensure the response body matches the mocked data
60+
assert response.headers["mcp-session-id"] is not None

0 commit comments

Comments
 (0)