_build_request() — anthropic-sdk-python Function Reference
Architecture documentation for the _build_request() function in _base_client.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD c1255cc8_bc9d_85e2_d7e7_d99e63ebb389["_build_request()"] 842ce0be_ea56_b939_960f_13eb61230daf["BaseClient"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|defined in| 842ce0be_ea56_b939_960f_13eb61230daf 2cb1e2bf_1a02_d5ea_42a9_c0def162363a["request()"] 2cb1e2bf_1a02_d5ea_42a9_c0def162363a -->|calls| c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 c21c2971_d83f_0afc_9811_0642b34cfcd3["request()"] c21c2971_d83f_0afc_9811_0642b34cfcd3 -->|calls| c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 56ae8c4c_37ad_68f4_21ce_35640dd2a264["_build_headers()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| 56ae8c4c_37ad_68f4_21ce_35640dd2a264 0a2c9ce8_a659_23ba_622c_074027fb8d75["_serialize_multipartform()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| 0a2c9ce8_a659_23ba_622c_074027fb8d75 4d73bd62_047b_924c_524a_7c9b60a2ca98["_prepare_url()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| 4d73bd62_047b_924c_524a_7c9b60a2ca98 99f35417_9c72_7af2_3e1d_6bc79dcd60df["model_dump()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| 99f35417_9c72_7af2_3e1d_6bc79dcd60df 2c40ea6c_a4cd_eacc_98e4_6c1614da6780["_merge_mappings()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| 2c40ea6c_a4cd_eacc_98e4_6c1614da6780 bf6a5442_2bf0_bd51_b523_e2e0677623fb["stringify()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| bf6a5442_2bf0_bd51_b523_e2e0677623fb afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7["get()"] c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 -->|calls| afc80c40_7ec9_e8a7_8063_f2ab74bc2ee7 style c1255cc8_bc9d_85e2_d7e7_d99e63ebb389 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/anthropic/_base_client.py lines 490–592
def _build_request(
self,
options: FinalRequestOptions,
*,
retries_taken: int = 0,
) -> httpx.Request:
if log.isEnabledFor(logging.DEBUG):
log.debug(
"Request options: %s",
model_dump(
options,
exclude_unset=True,
# Pydantic v1 can't dump every type we support in content, so we exclude it for now.
exclude={
"content",
}
if PYDANTIC_V1
else {},
),
)
kwargs: dict[str, Any] = {}
json_data = options.json_data
if options.extra_json is not None:
if json_data is None:
json_data = cast(Body, options.extra_json)
elif is_mapping(json_data):
json_data = _merge_mappings(json_data, options.extra_json)
else:
raise RuntimeError(f"Unexpected JSON data type, {type(json_data)}, cannot merge with `extra_body`")
headers = self._build_headers(options, retries_taken=retries_taken)
params = _merge_mappings(self.default_query, options.params)
content_type = headers.get("Content-Type")
files = options.files
# If the given Content-Type header is multipart/form-data then it
# has to be removed so that httpx can generate the header with
# additional information for us as it has to be in this form
# for the server to be able to correctly parse the request:
# multipart/form-data; boundary=---abc--
if content_type is not None and content_type.startswith("multipart/form-data"):
if "boundary" not in content_type:
# only remove the header if the boundary hasn't been explicitly set
# as the caller doesn't want httpx to come up with their own boundary
headers.pop("Content-Type")
# As we are now sending multipart/form-data instead of application/json
# we need to tell httpx to use it, https://www.python-httpx.org/advanced/clients/#multipart-file-encoding
if json_data:
if not is_dict(json_data):
raise TypeError(
f"Expected query input to be a dictionary for multipart requests but got {type(json_data)} instead."
)
kwargs["data"] = self._serialize_multipartform(json_data)
# httpx determines whether or not to send a "multipart/form-data"
# request based on the truthiness of the "files" argument.
# This gets around that issue by generating a dict value that
# evaluates to true.
#
# https://github.com/encode/httpx/discussions/2399#discussioncomment-3814186
if not files:
files = cast(HttpxRequestFiles, ForceMultipartDict())
prepared_url = self._prepare_url(options.url)
if "_" in prepared_url.host:
# work around https://github.com/encode/httpx/discussions/2880
kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")}
is_body_allowed = options.method.lower() != "get"
if is_body_allowed:
if options.content is not None and json_data is not None:
raise TypeError("Passing both `content` and `json_data` is not supported")
if options.content is not None and files is not None:
raise TypeError("Passing both `content` and `files` is not supported")
if options.content is not None:
kwargs["content"] = options.content
elif isinstance(json_data, bytes):
kwargs["content"] = json_data
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does _build_request() do?
_build_request() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/_base_client.py.
Where is _build_request() defined?
_build_request() is defined in src/anthropic/_base_client.py at line 490.
What does _build_request() call?
_build_request() calls 7 function(s): _build_headers, _merge_mappings, _prepare_url, _serialize_multipartform, get, model_dump, stringify.
What calls _build_request()?
_build_request() is called by 2 function(s): request, request.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free