fix(anthropic): clean up null file_id fields in citations during message formatting (#32592)

When citations are returned from streaming, they include a `file_id:
null` field in their `content_block_location` structure.

When these citations are passed back to the API in subsequent messages,
the API rejects them with "Extra inputs are not permitted" for the
`file_id` field.
This commit is contained in:
Mason Daugherty 2025-08-18 13:01:52 -04:00 committed by GitHub
parent fb74265175
commit 8042b04da6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -397,14 +397,23 @@ def _format_messages(
# accepted. # accepted.
# https://github.com/anthropics/anthropic-sdk-python/issues/461 # https://github.com/anthropics/anthropic-sdk-python/issues/461
if text.strip(): if text.strip():
content.append( formatted_block = {
{ k: v
k: v for k, v in block.items()
for k, v in block.items() if k in ("type", "text", "cache_control", "citations")
if k }
in ("type", "text", "cache_control", "citations") # Clean up citations to remove null file_id fields
}, if formatted_block.get("citations"):
) cleaned_citations = []
for citation in formatted_block["citations"]:
cleaned_citation = {
k: v
for k, v in citation.items()
if not (k == "file_id" and v is None)
}
cleaned_citations.append(cleaned_citation)
formatted_block["citations"] = cleaned_citations
content.append(formatted_block)
elif block["type"] == "thinking": elif block["type"] == "thinking":
content.append( content.append(
{ {