Skip to content

Commit baffa43

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

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

databricks/sdk/mixins/open_ai_client.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from requests import Response
55

66
from databricks.sdk.service.serving import (ExternalFunctionRequestHttpMethod,
7+
HttpRequestResponse,
78
ServingEndpointsAPI)
89

910

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

113+
# Create HttpRequestResponse from the raw response
114+
server_response = HttpRequestResponse.from_dict(res)
115+
100116
# Read the content from the HttpRequestResponse object
101117
if hasattr(server_response, "contents") and hasattr(server_response.contents, "read"):
102118
raw_content = server_response.contents.read() # Read the bytes
@@ -109,4 +125,9 @@ def http_request(
109125
else:
110126
raise ValueError("Contents must be bytes.")
111127

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

0 commit comments

Comments
 (0)