MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / _build_proxy_response

Function _build_proxy_response

uncommon_route/proxy.py:4999–5065  ·  view source on GitHub ↗
(
            *,
            status_code: int,
            content: bytes,
            content_type: str,
            native_transport: bool,
        )

Source from the content-addressed store, hash-verified

4997 return _transport_error_response(exc)
4998
4999 def _build_proxy_response(
5000 *,
5001 status_code: int,
5002 content: bytes,
5003 content_type: str,
5004 native_transport: bool,
5005 ) -> Response:
5006 if api_format == "anthropic":
5007 if native_transport:
5008 return Response(
5009 content=content,
5010 status_code=status_code,
5011 headers={
5012 "content-type": content_type,
5013 **debug_headers,
5014 },
5015 )
5016 if status_code == 200:
5017 try:
5018 oai_data = json.loads(content)
5019 anth_data = openai_to_anthropic_response(
5020 oai_data,
5021 _anthropic_response_model_name(
5022 response_model or requested_model or selected_model
5023 ),
5024 )
5025 return JSONResponse(anth_data, headers=debug_headers)
5026 except (json.JSONDecodeError, KeyError, IndexError):
5027 pass
5028 try:
5029 err_body = json.loads(content)
5030 err_msg = err_body.get("error", {}).get("message", "Upstream error")
5031 except (json.JSONDecodeError, TypeError):
5032 err_msg = "Upstream error"
5033 return JSONResponse(
5034 anthropic_error_response(status_code, err_msg),
5035 status_code=status_code,
5036 headers=debug_headers,
5037 )
5038
5039 if native_transport:
5040 if status_code == 200:
5041 try:
5042 anth_data = json.loads(content)
5043 oai_data = anthropic_to_openai_response(anth_data, selected_model)
5044 return JSONResponse(oai_data, headers=debug_headers)
5045 except (json.JSONDecodeError, KeyError, IndexError, TypeError):
5046 pass
5047 try:
5048 err_body = json.loads(content)
5049 err_msg = err_body.get("error", {}).get("message", "Upstream error")
5050 except (json.JSONDecodeError, TypeError):
5051 err_msg = "Upstream error"
5052 return JSONResponse(
5053 {"error": {"message": err_msg, "type": "proxy_error"}},
5054 status_code=status_code,
5055 headers=debug_headers,
5056 )

Callers 2

_select_stream_responseFunction · 0.85
_handle_chat_coreFunction · 0.85

Calls 5

anthropic_error_responseFunction · 0.90
getMethod · 0.45

Tested by

no test coverage detected