AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Type Contract Violations In Import_attr And Content_blocks Allow Invalid Values And Return Types

langchain-core 1.6.6 contains two type contract inconsistencies: import_attr accepts package=None but raises TypeError due to always using relative imports, and AIMessage/AIMessageChunk.content_blocks return list[str] when content is a list of strings, violating the declared return type list[ContentBlock].

highConfidence 92%Langchain-CoreAffected V1.6.6

Origin Analysis

1) import_attr always calls importlib.import_module(f'.{module_name}', package), which requires a non-None package; the type hint package: str | None allows None, leading to TypeError. 2) The content_blocks property only converts string content to a list of text blocks but does not normalize individual string elements inside a list, returning the raw list of strings despite the type annotation.
1) Run import_attr('x', 'y', None) with langchain_core imported; observe TypeError: the 'package' argument is required to perform a relative import. 2) Create AIMessage or AIMessageChunk with content=['hello'] and response_metadata={'output_version': 'v1'}; print message.content_blocks; observe ['hello'] (list of str) instead of list of dict blocks.

Fixing Code Block

Edge Case Audit

Changing import_attr may alter behavior for callers that passed package=None expecting TypeError; now they will trigger absolute imports, which could import an unintended module if module_name is not fully qualified. Changing content_blocks will modify return type for list[str] inputs: previously list[str], now list[dict]; any downstream code that assumes list[str] elements will break. Dict elements are not validated against the ContentBlock TypedDict, so malformed dicts could still violate the type hint. Rollback: revert to original property or pin langchain-core==1.6.6. Test with integrations that consume content_blocks (e.g., Anthropic, OpenAI tool calling) before deploying.

Ecosystem Topology