CVE-2026-40087 LangChain has incomplete f-string validation in prompt templates

Medium 5.3 Published Updated

Incomplete f-string validation in LangChain prompt templates allows attackers to execute arbitrary code via crafted templates.

Exploited in the wild
Not known
Login needed
Not needed
Attackable
Over the internet
Victim action needed
Not needed
Fix
Available

What is CVE-2026-40087?

CVE-2026-40087 is a medium-severity code injection vulnerability in langchain-ai langchain, published on 9 Apr 2026. An attacker can exploit it over the internet without logging in, with no action from the victim.

Official description

LangChain is a framework for building agents and LLM-powered applications. Prior to 0.3.84 and 1.2.28, LangChain's f-string prompt-template validation was incomplete in two respects. First, some prompt template classes accepted f-string templates and formatted them without enforcing the same attribute-access validation as PromptTemplate. In particular, DictPromptTemplate and ImagePromptTemplate could accept templates containing attribute access or indexing expressions and subsequently evaluate those expressions during formatting. Second, f-string validation based on parsed top-level field names did not reject nested replacement fields inside format specifiers. In this pattern, the nested replacement field appears in the format specifier rather than in the top-level field name. As a result, earlier validation based on parsed field names did not reject the template even though Python formatting would still attempt to resolve the nested expression at runtime. This vulnerability is fixed in 0.3.84 and 1.2.28.

Weakness: CWE-1336

Which versions are affected by CVE-2026-40087?

5 open source packages ship an affected version, including langchain-core. It affects 1 product.

PackageAffectedFixed in
langchain-core PyPIAll versions0.3.84, 1.2.28
langchain-core PyPIAll versions0.3.84
langchain-core PyPIAll versions0.3.84
langchain-core PyPIFrom 1.0.0a11.2.28
langchain-core PyPIFrom 1.0.0a11.2.28

Products include Langchain Langchain Core.

How do I fix CVE-2026-40087?

Upgrade langchain-core to a fixed version for your release line: 0.3.84 or 1.2.28.

Upgrade, and never evaluate templates, expressions or code built from user input. More about Code injection.

Is CVE-2026-40087 being exploited?

There is no known exploitation of CVE-2026-40087 in the wild and no public exploit code.

Sources

Also known as ECHO-8479-4c4c-18a0, GHSA-926x-3r5x-gfhw, PYSEC-2026-2563, ROOT-APP-PYPI-CVE-2026-40087.

Is CVE-2026-40087 in your software?

Upload an SBOM and sbomify checks it against every vulnerability here, every day.

Check your SBOMs for free