Chat Widget example not working with Pydantic > 2 and langchain > 0.1.1 #101

Closed
opened 2026-02-16 00:18:47 -05:00 by yindo · 1 comment
Owner

Originally created by @dentroai on GitHub (Jan 23, 2024).

The code taken from https://python.langchain.com/docs/langserve#chat-widget doesn't work with pydantic > 2 and langchain > 0.1.1

It always throws: TypeError: Object of type 'FieldInfo' is not JSON serializable

Code from the docs:

from langserve.schema import CustomUserType

from typing import List, Tuple
from pydantic import Field
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage
from langchain_core.runnables import RunnableLambda, RunnableParallel, RunnablePassthrough, ConfigurableField
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from operator import itemgetter

from dotenv import load_dotenv
load_dotenv()

class ChatHistory(CustomUserType):
    chat_history: List[Tuple[str, str]] = Field(
        ...,
        examples=[[("human input", "ai response")]],
        extra={"widget": {"type": "chat", "input": "question", "output": "answer"}},
    )
    question: str


def _format_to_messages(input: ChatHistory) -> List[BaseMessage]:
    """Format the input to a list of messages."""
    history = input.chat_history
    user_input = input.question

    messages = []

    for human, ai in history:
        messages.append(HumanMessage(content=human))
        messages.append(AIMessage(content=ai))
    messages.append(HumanMessage(content=user_input))
    return messages

model = ChatOpenAI()

chat_model = (
    RunnableLambda(_format_to_messages) 
    | model
    | {"answer": RunnablePassthrough()}
).with_types(input_type=ChatHistory)
Originally created by @dentroai on GitHub (Jan 23, 2024). The code taken from https://python.langchain.com/docs/langserve#chat-widget doesn't work with pydantic > 2 and langchain > 0.1.1 It always throws: `TypeError: Object of type 'FieldInfo' is not JSON serializable` Code from the docs: ``` from langserve.schema import CustomUserType from typing import List, Tuple from pydantic import Field from langchain_core.messages import BaseMessage, HumanMessage, AIMessage from langchain_core.runnables import RunnableLambda, RunnableParallel, RunnablePassthrough, ConfigurableField from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser from operator import itemgetter from dotenv import load_dotenv load_dotenv() class ChatHistory(CustomUserType): chat_history: List[Tuple[str, str]] = Field( ..., examples=[[("human input", "ai response")]], extra={"widget": {"type": "chat", "input": "question", "output": "answer"}}, ) question: str def _format_to_messages(input: ChatHistory) -> List[BaseMessage]: """Format the input to a list of messages.""" history = input.chat_history user_input = input.question messages = [] for human, ai in history: messages.append(HumanMessage(content=human)) messages.append(AIMessage(content=ai)) messages.append(HumanMessage(content=user_input)) return messages model = ChatOpenAI() chat_model = ( RunnableLambda(_format_to_messages) | model | {"answer": RunnablePassthrough()} ).with_types(input_type=ChatHistory) ```
yindo closed this issue 2026-02-16 00:18:47 -05:00
Author
Owner

@dentroai commented on GitHub (Jan 23, 2024):

Apparently adding this import fixed it (taken from the example in the docs of the file upload widget: https://python.langchain.com/docs/langserve#file-upload-widget):

try:
    from pydantic.v1 import Field
except ImportError:
    from pydantic import Field
@dentroai commented on GitHub (Jan 23, 2024): Apparently adding this import fixed it (taken from the example in the docs of the file upload widget: https://python.langchain.com/docs/langserve#file-upload-widget): ``` try: from pydantic.v1 import Field except ImportError: from pydantic import Field ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#101