new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

LangChain · Agents · all subjects

agents/model

29 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Model parameter accepts string identifier or instance

The model parameter can accept either a model identifier string in the format 'provider:model' or an initialized model instance. Both forms are valid when creating an agent.

Model context components

Model context is what goes into each model call and consists of five components: System Prompt (base instructions from the developer to the LLM), Messages (the full list of messages/conversation history sent to the LLM), Tools (utilities the agent has access to for taking actions), Model (the actual model including configuration to be called), and Response Format (schema specification for the model's final response). All of these can draw from state (short-term memory), store (long-term memory), or runtime context (static configuration).

Dynamic model selection based on conversation length

Example: Use @wrap_model_call to check message_count from request.messages. If message_count > 20, use large_model (larger context window). If message_count > 10, use standard_model. Otherwise use efficient_model. Use request.override(model=selected_model). Initialize models once outside the middleware.

Dynamic model selection based on store preferences

Example: Use @wrap_model_call to read user preferences from store via request.runtime.store.get(('preferences',), user_id). Extract preferred_model and map to MODEL_MAP dictionary. Use request.override(model=MODEL_MAP[preferred_model]) if the model exists.

Dynamic model selection based on cost tier and environment

Example: Use @wrap_model_call to read cost_tier and environment from request.runtime.context. If environment == 'production' and cost_tier == 'premium', use premium_model. If cost_tier == 'budget', use budget_model. Otherwise use standard_model. Use request.override(model=selected_model).

Example: Pass API key explicitly to ChatOpenAI in TypeScript

import { ChatOpenAI } from "@langchain/openai"; const model = new ChatOpenAI({ apiKey: "YOUR_KEY_HERE", });

Example: Pass API key explicitly to ChatOpenAI in Python

from langchain_openai import ChatOpenAI model = ChatOpenAI(api_key="YOUR_KEY_HERE")

MODEL_AUTHENTICATION error - when and why it occurs

The MODEL_AUTHENTICATION error is currently only used in langchainjs (JavaScript/TypeScript). It occurs when your model provider denies you access to their service, typically due to an issue with authentication credentials or API keys.

MODEL_AUTHENTICATION troubleshooting - credential validation

To troubleshoot MODEL_AUTHENTICATION errors: confirm that your API key or authentication credentials are accurate and valid. If using environment-based authentication, verify that the variable name is spelled correctly, the variable contains an assigned value, and third-party packages like dotenv haven't interfered with loading.

MODEL_AUTHENTICATION troubleshooting - proxy and custom endpoints

When using a proxy or non-standard endpoint, verify that your custom provider does not expect an alternative authentication scheme.

Pass API credentials explicitly to bypass environment variable issues

You can bypass environment variable issues by passing credentials explicitly to the model constructor. In Python, pass api_key="YOUR_KEY_HERE" to ChatOpenAI. In TypeScript, pass apiKey: "YOUR_KEY_HERE" to the ChatOpenAI constructor.

Accepted message formats in LangChain

LangChain accepts the following message formats: OpenAI style message objects with role and content keys (e.g., { role: "user", content: "Hello world!" }), tuples, LangChain message classes like BaseMessage, and plain strings which are automatically converted to HumanMessage objects.

Message dict validation requires role and content keys

When passing message objects as dictionaries to chat models, they must contain exactly 'role' and 'content' keys. Other keys or missing keys will cause a ValueError. For example, { role: "HumanMessage", random_field: "random value" } will fail because 'content' is missing.

Troubleshooting MESSAGE_COERCION_FAILURE errors

To resolve MESSAGE_COERCION_FAILURE errors: (1) ensure all inputs to chat models are an array of LangChain message classes or a supported message-like format, (2) verify that messages are not unintentionally stringified or transformed before being passed to the model, (3) examine the error's stack trace and add logging statements to inspect message objects before they reach the model.

MESSAGE_COERCION_FAILURE example: invalid dict structure

Example that triggers MESSAGE_COERCION_FAILURE: from langchain_anthropic import ChatAnthropic uncoercible_message = {"role": "HumanMessage", "random_field": "random value"} model = ChatAnthropic(model="claude-sonnet-4-6") model.invoke([uncoercible_message]) This raises: ValueError: Message dict must contain 'role' and 'content' keys, got {'role': 'HumanMessage', 'random_field': 'random value'}

MessageLikeRepresentation type definition

LangChain modules accept MessageLikeRepresentation, which is defined as a Union type that includes: BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate, tuples with (Union[str, type], Union[str, list[dict], list[object]]), or plain strings. This type is imported from langchain_core.prompts.chat.

MESSAGE_COERCION_FAILURE error conditions

The MESSAGE_COERCION_FAILURE error occurs when message objects passed to LangChain modules do not conform to the expected MessageLikeRepresentation format. For example, a message dict missing required 'role' and 'content' keys, or containing unexpected fields, will trigger this error with a ValueError stating which keys are missing or which keys were received.

MODEL_NOT_FOUND troubleshooting: check proxy configurations

To resolve MODEL_NOT_FOUND errors, check proxy or wrapper configurations. If using a proxy or alternative host with a model wrapper, confirm that permitted model names are not restricted or altered.

MODEL_NOT_FOUND error

The MODEL_NOT_FOUND error occurs when the model name specified is not acknowledged by the provider. This error is currently only used in langchainjs (JavaScript/TypeScript).

MODEL_NOT_FOUND troubleshooting: verify model identifier

To resolve MODEL_NOT_FOUND errors, first verify the model identifier by double-checking the model string being passed in. Ensure the spelling and format are correct.

MODEL_NOT_FOUND causes

The MODEL_NOT_FOUND error typically stems from either a typo in the model name string itself or restrictions imposed by a proxy service or model wrapper between the code and the provider's API.

MODEL_RATE_LIMIT error definition

The MODEL_RATE_LIMIT error occurs when you exceed the maximum number of requests permitted by your model provider within a specific timeframe, resulting in temporary blocking. The restriction is generally temporary and lifts after the limit resets. This error is currently only used in langchainjs (JavaScript/TypeScript).

Troubleshooting MODEL_RATE_LIMIT errors

To resolve MODEL_RATE_LIMIT errors, you can implement rate limiting to regulate the frequency of requests sent to the model, implement response caching to reduce redundant requests when incoming queries are repetitive, use multiple providers to distribute requests across them if your application architecture supports this approach, or contact your model provider requesting an increase to your rate limits.

Initialization of chat model with init_chat_model or ChatOpenAI

Python uses init_chat_model('provider:model_name') for brevity, while JavaScript typically uses new ChatOpenAI({model: 'model_name'}) or other provider classes. The model is passed to create_agent/createAgent as the first argument.

Supported model providers in create_agent

create_agent supports OpenAI, Anthropic, Google, and more. Specific model identifiers include: openai:gpt-5.5, google_genai:gemini-2.5-flash-lite, claude-sonnet-4-6, openrouter:anthropic/claude-sonnet-4-6, fireworks:accounts/fireworks/models/qwen3p5-397b-a17b, baseten:zai-org/GLM-5.2, ollama:devstral-2, azure_openai:gpt-5.5, bedrock_converse:us.anthropic.claude-sonnet-4-6, huggingface:microsoft/Phi-3-mini-4k-instruct.

OpenAI Chat Completion API impact on LangChain

In January 2023, OpenAI released the Chat Completion API, which evolved from taking strings and returning strings to taking lists of messages and returning messages. Other model providers followed suit, and LangChain updated to work with lists of messages.

Model APIs multimodal evolution

In April 2025, model APIs became more multimodal, starting to accept files, images, videos, and more. The `langchain-core` (Python) and `@langchain/core` (JavaScript) message formats were updated to allow developers to specify these multimodal inputs in a standard way.

Recommended model type for SQL agents

SQL agents should use a model that supports tool-calling capabilities.

LangChain agent initialization for voice

Voice agents are created with create_agent() function, specifying: model (e.g., google_genai:gemini-3.6-flash), tools as a list of functions, system_prompt (should not use emojis, special characters, or markdown since responses go to TTS), and checkpointer=InMemorySaver() for conversation memory.

Give your agent this brain