langchain-ai/langchain · error · NotImplementedError

Structured prompts need to be piped to a language model.

Error message

Structured prompts need to be piped to a language model.

What it means

Raised by `StructuredPrompt.__or__` (the `|` pipe operator) when the object being piped is neither recognized as a `BaseLanguageModel` nor exposes a `with_structured_output` method. StructuredPrompt works by piping into a model and wrapping it with `with_structured_output(self.schema_, ...)`, so anything else cannot honor the schema contract and raises `NotImplementedError`.

Source

Thrown at libs/core/langchain_core/prompts/structured.py:214

            A `RunnableSequence` object.

        Raises:
            NotImplementedError: If the first element of `others` is not a language
                model.
        """
        if (others and isinstance(others[0], BaseLanguageModel)) or hasattr(
            others[0], "with_structured_output"
        ):
            return RunnableSequence(
                self,
                others[0].with_structured_output(
                    self.schema_, **self.structured_output_kwargs
                ),
                *others[1:],
                name=name,
            )
        msg = "Structured prompts need to be piped to a language model."
        raise NotImplementedError(msg)

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Pipe the structured prompt directly into a language model first: `prompt | llm`, and append any parser after the model: `prompt | llm | parser`
  2. For custom model classes, implement `with_structured_output(schema, **kwargs)` or subclass `BaseLanguageModel` so the isinstance check passes
  3. If you do not need structured output, use a plain `ChatPromptTemplate` instead of `StructuredPrompt`

Example fix

# before
chain = structured_prompt | StrOutputParser()  # NotImplementedError

# after
chain = structured_prompt | llm | StrOutputParser()  # model first, then parser
Defensive patterns

Strategy: type-guard

Validate before calling

from langchain_core.language_models import BaseLanguageModel

def accepts_structured_prompt(target: object) -> bool:
    return isinstance(target, BaseLanguageModel) or hasattr(target, "with_structured_output")

Type guard

from langchain_core.language_models import BaseLanguageModel

def is_pipeable_model(x: object) -> bool:
    return isinstance(x, BaseLanguageModel) or callable(getattr(x, "with_structured_output", None))

Try / catch

try:
    chain = structured_prompt | target
except NotImplementedError as e:
    if "language model" in str(e):
        raise TypeError("pipe StructuredPrompt into a model first: prompt | llm | parser") from e
    raise

Prevention

When it happens

Trigger: `structured_prompt | output_parser` (e.g. `StrOutputParser`), `structured_prompt | some_runnable`, or piping to a mock/test double lacking `with_structured_output`. The condition checks `others[0]` only, so a model anywhere but first position also fails.

Common situations: Adding an output parser directly after a structured prompt (a habit from normal LCEL chains where `prompt | llm | parser` is idiomatic — here the parser must come after the model, not instead of it); unit tests piping to fake runnables; custom model wrappers that did not inherit from `BaseLanguageModel` and do not implement `with_structured_output`.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/c5d8b82defa7ed8b. Report an issue: GitHub.