Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Chat Input component #2230

Open
sorenaakjaer opened this issue Jun 19, 2024 · 6 comments
Open

Issue with Chat Input component #2230

sorenaakjaer opened this issue Jun 19, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@sorenaakjaer
Copy link

sorenaakjaer commented Jun 19, 2024

Describe the bug
I have just installed Langflow and made the very simple flow only "ChatInput" and the "ChatOutput" compoent.

But when I run it I'm getteing the following error of the output of the ChatInput module.

In the terminal window i se the following:
_"ValidationError: 3 validation errors for Message
text.str
Input should be a valid string [type=string_type,
input_value=Message(text='Hey', sende...19:10:36', flow_id=None),
input_type=Message]
For further information visit
https://errors.pydantic.dev/2.7/v/string_type
text.is-instance[AsyncIterator]
Input should be an instance of AsyncIterator [type=is_instance_of,
input_value=Message(text='Hey', sende...19:10:36', flow_id=None),
input_type=Message]
For further information visit
https://errors.pydantic.dev/2.7/v/is_instance_of
text.is-instance[Iterator]
Input should be an instance of Iterator [type=is_instance_of,
input_value=Message(text='Hey', sende...19:10:36', flow_id=None),
input_type=Message]
For further information visit
https://errors.pydantic.dev/2.7/v/is_instance_of

                         The above exception was the direct cause of the following exception:

                         ┌──────────────────── Traceback (most recent call last) ─────────────────────┐
                         │ C:\Program Files\Python310\lib\site-packages\langflow\api\v1\chat.py:175   │
                         │ in build_vertex                                                            │
                         │                                                                            │
                         │   172 │   │   │   │   valid,                                               │
                         │   173 │   │   │   │   artifacts,                                           │
                         │   174 │   │   │   │   vertex,                                              │
                         │ > 175 │   │   │   ) = await graph.build_vertex(                            │
                         │   176 │   │   │   │   lock=lock,                                           │
                         │   177 │   │   │   │   chat_service=chat_service,                           │
                         │   178 │   │   │   │   vertex_id=vertex_id,                                 │
                         │                                                                            │
                         │ C:\Program                                                                 │
                         │ Files\Python310\lib\site-packages\langflow\graph\graph\base.py:780 in      │
                         │ build_vertex                                                               │
                         │                                                                            │
                         │    777 │   │   │   logger.exception(f"Error building Component: {exc}")    │
                         │    778 │   │   │   flow_id = self.flow_id                                  │
                         │    779 │   │   │   log_transaction(flow_id, vertex, status="failure", erro │
                         │ >  780 │   │   │   raise exc                                               │
                         │    781 │                                                                   │
                         │    782 │   async def get_next_and_top_level_vertices(                      │
                         │    783 │   │   self,                                                       │
                         │                                                                            │
                         │ C:\Program                                                                 │
                         │ Files\Python310\lib\site-packages\langflow\graph\graph\base.py:757 in      │
                         │ build_vertex                                                               │
                         │                                                                            │
                         │    754 │   │   │   │   │   │   vertex.result.used_frozen_result = True     │
                         │    755 │   │   │                                                           │
                         │    756 │   │   │   else:                                                   │
                         │ >  757 │   │   │   │   await vertex.build(                                 │
                         │    758 │   │   │   │   │   user_id=user_id, inputs=inputs_dict,            │
                         │        fallback_to_env_vars=fallback_to_env_vars, files=files              │
                         │    759 │   │   │   │   )                                                   │
                         │    760 │   │   │   │   await chat_service.set_cache(key=vertex.id, data=ve │
                         │                                                                            │
                         │ C:\Program                                                                 │
                         │ Files\Python310\lib\site-packages\langflow\graph\vertex\base.py:705 in     │
                         │ build                                                                      │
                         │                                                                            │
                         │   702 │   │   │   for step in self.steps:                                  │
                         │   703 │   │   │   │   if step not in self.steps_ran:                       │
                         │   704 │   │   │   │   │   if inspect.iscoroutinefunction(step):            │
                         │ > 705 │   │   │   │   │   │   await step(user_id=user_id, **kwargs)        │
                         │   706 │   │   │   │   │   else:                                            │
                         │   707 │   │   │   │   │   │   step(user_id=user_id, **kwargs)              │
                         │   708 │   │   │   │   │   self.steps_ran.append(step)                      │
                         │                                                                            │
                         │ C:\Program                                                                 │
                         │ Files\Python310\lib\site-packages\langflow\graph\vertex\base.py:409 in     │
                         │ _build                                                                     │
                         │                                                                            │
                         │   406 │   │   """                                                          │
                         │   407 │   │   logger.debug(f"Building {self.display_name}")                │
                         │   408 │   │   await self._build_each_vertex_in_params_dict(user_id)        │
                         │ > 409 │   │   await self._get_and_instantiate_class(user_id, fallback_to_e │
                         │   410 │   │   self._validate_built_object()                                │
                         │   411 │   │                                                                │
                         │   412 │   │   self._built = True                                           │
                         │                                                                            │
                         │ C:\Program                                                                 │
                         │ Files\Python310\lib\site-packages\langflow\graph\vertex\base.py:621 in     │
                         │ _get_and_instantiate_class                                                 │
                         │                                                                            │
                         │   618 │   │   except Exception as exc:                                     │
                         │   619 │   │   │   logger.exception(exc)                                    │
                         │   620 │   │   │                                                            │
                         │ > 621 │   │   │   raise ValueError(f"Error building Component {self.displa │
                         │       {str(exc)}") from exc                                                │
                         │   622 │                                                                    │
                         │   623 │   def _update_built_object_and_artifacts(self, result):            │
                         │   624 │   │   """                                                          │
                         └────────────────────────────────────────────────────────────────────────────┘
                         ValueError: Error building Component Chat Output: 3 validation errors for
                         Message
                         text.str
                           Input should be a valid string [type=string_type,
                         input_value=Message(text='Hey', sende...19:10:36', flow_id=None),
                         input_type=Message]
                             For further information visit
                         https://errors.pydantic.dev/2.7/v/string_type
                         text.is-instance[AsyncIterator]
                           Input should be an instance of AsyncIterator [type=is_instance_of,
                         input_value=Message(text='Hey', sende...19:10:36', flow_id=None),
                         input_type=Message]
                             For further information visit
                         https://errors.pydantic.dev/2.7/v/is_instance_of
                         text.is-instance[Iterator]
                           Input should be an instance of Iterator [type=is_instance_of,
                         input_value=Message(text='Hey', sende...19:10:36', flow_id=None),
                         input_type=Message]
                             For further information visit
                         https://errors.pydantic.dev/2.7/v/is_instance_of"_

I have tested towards other compoents and it seems to be a problem format of the outfrom the "ChatInput" compoent.

Browser and Version

  • Browser: Chrome
  • Version: Version 126.0.6478.62

To Reproduce
Steps to reproduce the behavior:

  1. Drag ChatInput
  2. Drag ChatOutput
  3. Draw flow line between ChatInput outout to ChatOutput input
  4. Test flow

Screenshots
image

Additional context
Here a the code if te ChatInput component

from typing import Optional

from langflow.base.io.chat import ChatComponent
from langflow.schema.message import Message
from langflow.field_typing import Text
from typing import Union


class ChatInput(ChatComponent):
    display_name = "Chat Input"
    description = "Get chat inputs from the Playground."
    icon = "ChatInput"

    def build_config(self):
        build_config = super().build_config()
        build_config["input_value"] = {
            "input_types": [],
            "display_name": "Text",
            "multiline": True,
        }
        build_config["return_message"] = {
            "display_name": "Return Record",
            "advanced": True,
        }

        return build_config

    def build(
        self,
        sender: Optional[str] = "User",
        sender_name: Optional[str] = "User",
        input_value: Optional[str] = None,
        files: Optional[list[str]] = None,
        session_id: Optional[str] = None,
        return_message: Optional[bool] = True,
    ) -> Union[Message, Text]:
        return super().build_with_record(
            sender=sender,
            sender_name=sender_name,
            input_value=input_value,
            files=files,
            session_id=session_id,
            return_message=return_message,
        )

@dosubot dosubot bot added the bug Something isn't working label Jun 19, 2024
Copy link

dosubot bot commented Jun 19, 2024

Hello @sorenaakjaer! I'm here to help you with any bugs, questions, or contributions you have for the Langflow repository. Let me know how I can assist you!

The error you're encountering with the "ChatInput" and "ChatOutput" components in Langflow is due to validation issues with the Message class. Specifically, the text field in the Message class is not being recognized as a valid string, and there are additional checks for AsyncIterator and Iterator types.

To resolve this issue, ensure that the text field in the Message class is correctly formatted as a string. Here are the key points to check:

  1. Ensure text is a String: The text field should be a valid string. If it's not, you need to convert it to a string before passing it to the ChatOutput component.

  2. Validation Rules: The Message class has specific validation rules to ensure the text field is not an AsyncIterator or Iterator. Here is the relevant code snippet for the Message class:

    @field_serializer("text", mode="plain")
    def serialize_text(self, value):
        if isinstance(value, AsyncIterator):
            return ""
        elif isinstance(value, Iterator):
            return ""
        return value
  3. Correct Parameters: Ensure that the parameters passed to the build method of the ChatOutput component are correctly provided and match the expected types. Here is the relevant code snippet for the ChatOutput component:

    from typing import Optional, Union
    
    from langflow.base.io.chat import ChatComponent
    from langflow.field_typing import Text
    from langflow.schema import Record
    
    class ChatOutput(ChatComponent):
        display_name = "Chat Output"
        description = "Display a chat message in the Interaction Panel."
        icon = "ChatOutput"
    
        def build(
            self,
            sender: Optional[str] = "Machine",
            sender_name: Optional[str] = "AI",
            input_value: Optional[str] = None,
            session_id: Optional[str] = None,
            return_record: Optional[bool] = False,
            record_template: Optional[str] = "{text}",
        ) -> Union[Text, Record]:
            return super().build_with_record(
                sender=sender,
                sender_name=sender_name,
                input_value=input_value,
                session_id=session_id,
                return_record=return_record,
                record_template=record_template or "",
            )

Make sure that when you are using the ChatOutput component, you provide the necessary parameters such as sender, sender_name, input_value, session_id, return_record, and record_template correctly. If any of these parameters are missing or incorrectly typed, it could lead to validation errors [1][2].

By ensuring the text field is a valid string and correctly passing the parameters, you should be able to resolve the validation error.

To continue talking to Dosu, mention @dosu.

@sorenaakjaer
Copy link
Author

Hi

The problem is not on the Chat Output component as it also fails if a drag the ChatInput component to a OpenAI Model

@sorenaakjaer
Copy link
Author

ChatGPT suggest the follwing:

`
from typing import Optional, Union
from langflow.base.io.chat import ChatComponent
from langflow.schema.message import Message
from langflow.field_typing import Text
from datetime import datetime
import logging

class ChatInput(ChatComponent):
display_name = "Chat Input"
description = "Get chat inputs from the Playground."
icon = "ChatInput"

def build_config(self):
    build_config = super().build_config()
    build_config["input_value"] = {
        "input_types": [],
        "display_name": "Text",
        "multiline": True,
    }
    build_config["return_message"] = {
        "display_name": "Return Record",
        "advanced": True,
    }
    return build_config

def build(
    self,
    sender: Optional[str] = "User",
    sender_name: Optional[str] = "User",
    input_value: Optional[str] = None,
    files: Optional[list[str]] = None,
    session_id: Optional[str] = None,
    return_message: Optional[bool] = True,
) -> Union[Message, Text]:
    if input_value is None:
        logging.error("input_value cannot be None")
        raise ValueError("input_value cannot be None")
    
    message = Message(
        text=input_value,
        sender=sender,
        sender_name=sender_name,
        timestamp=datetime.utcnow().isoformat(),
        flow_id=None
    )
    
    logging.debug(f"Built message: {message}")
    return message if return_message else input_value

`

It just seems strange that i need to make code adjustments to such a simple setup

@sorenaakjaer
Copy link
Author

I actually found a fix! If i add en "Text Output" in between there are not issue

image

@manirajanvn
Copy link

I had same issue, after disabling return record value in chat input advanced option, issue not occurred.

@anovazzi1
Copy link
Contributor

Hello,
Sorry for the delay. Did you try using the new version? Does the error still persist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants