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

Langchain-Openrouter Fails To Enforce Json_schema In With_structured_output

When using ChatOpenRouter.with_structured_output with method='json_schema', the schema is not passed to the OpenRouter API, causing the model to return arbitrary JSON that violates the schema.

highConfidence 75%LangChainChat Modelslangchain-coreAffected V0.1.0

Origin Analysis

The ChatOpenRouter integration does not correctly map the LangChain response_format to the OpenRouter API's expected format, so the JSON schema constraint is omitted from the request.
1. Install langchain-openrouter 0.1.0 and set OPENROUTER_API_KEY. 2. Define a JSON schema (dict or Pydantic model). 3. Instantiate ChatOpenRouter and call with_structured_output(schema, method='json_schema', strict=True). 4. Invoke with a sample text. Observe the output does not conform to the schema fields.

Fixing Code Block

Edge Case Audit

This is a temporary workaround. It requires manual output parsing and may break if the OpenRouter API does not support strict=True (it might be silently ignored). Official fix should be implemented in the library to properly handle response_format. Rollback: remove the bind call and revert to with_structured_output once the library is updated.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state