The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
feat(core): Autogenerate filenames for when converting file content blocks to OpenAI format (#30984)
Jacob Lee6b0b317cb5eb
diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.pyindex 6582f298ab..d3e27c502a 100644--- a/libs/core/langchain_core/messages/utils.py+++ b/libs/core/langchain_core/messages/utils.py@@ -12,6 +12,7 @@ from __future__ import annotationsimport base64import inspectimport json+import loggingimport mathfrom collections.abc import Iterable, Sequencefrom functools import partial@@ -47,6 +48,8 @@ if TYPE_CHECKING:from langchain_core.prompt_values import PromptValuefrom langchain_core.runnables.base import Runnable+logger = logging.getLogger(__name__)+def _get_type(v: Any) -> str:"""Get the type associated with the object for serialization purposes."""@@ -1070,7 +1073,15 @@ def convert_to_openai_messages()# Standard multi-modal content blockelif is_data_content_block(block):- content.append(convert_to_openai_data_block(block))+ formatted_block = convert_to_openai_data_block(block)+ if (+ formatted_block.get("type") == "file"+ and "file" in formatted_block+ and "filename" not in formatted_block["file"]+ ):+ logger.info("Generating a fallback filename.")+ formatted_block["file"]["filename"] = "LC_AUTOGENERATED"+ content.append(formatted_block)# Anthropic and Bedrock converse formatelif (block.get("type") == "image") or "image" in block:# Anthropicdiff --git a/libs/core/tests/unit_tests/messages/test_utils.py b/libs/core/tests/unit_tests/messages/test_utils.pyindex 9031e1be5d..db75921e1a 100644--- a/libs/core/tests/unit_tests/messages/test_utils.py+++ b/libs/core/tests/unit_tests/messages/test_utils.py@@ -1202,12 +1202,6 @@ def test_convert_to_openai_messages_multimodal() -> None:"data": "<base64 string>","mime_type": "image/png",},- {- "type": "file",- "source_type": "base64",- "data": "<base64 string>",- "mime_type": "application/pdf",- },{"type": "file","source_type": "base64",@@ -1232,7 +1226,34 @@ def test_convert_to_openai_messages_multimodal() -> None:result = convert_to_openai_messages(messages, text_format="block")assert len(result) == 1message = result[0]- assert len(message["content"]) == 7+ assert len(message["content"]) == 6++ # Test adding filename+ messages = [+ HumanMessage(+ content=[+ {+ "type": "file",+ "source_type": "base64",+ "data": "<base64 string>",+ "mime_type": "application/pdf",+ },+ ]+ )+ ]+ with pytest.warns(match="filename"):+ result = convert_to_openai_messages(messages, text_format="block")+ assert len(result) == 1+ message = result[0]+ assert len(message["content"]) == 1+ block = message["content"][0]+ assert block == {+ "type": "file",+ "file": {+ "file_data": "data:application/pdf;base64,<base64 string>",+ "filename": "LC_AUTOGENERATED",+ },+ }def test_count_tokens_approximately_empty_messages() -> None: