langchain-ai / langchain-cohere

MIT License
31 stars 20 forks source link

Pydantic basemodel with enum field not supported with_structured_output #9

Open gopinathan-av opened 5 months ago

gopinathan-av commented 5 months ago

llm.with_structured_output method does not support Pydantic models with enum fields. Getting following error.

    return master_prompts | self.get_default_model().with_structured_output(MessageRouter)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_core/_api/beta_decorator.py", line 109, in warning_emitting_wrapper
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/chat_models.py", line 210, in with_structured_output
    llm = self.bind_tools([schema], **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/chat_models.py", line 189, in bind_tools
    formatted_tools = _format_to_cohere_tools(tools)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 61, in _format_to_cohere_tools
    return [_convert_to_cohere_tool(tool) for tool in tools]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 61, in <listcomp>
    return [_convert_to_cohere_tool(tool) for tool in tools]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 155, in _convert_to_cohere_tool
    parameter_definitions={
                          ^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 156, in <dictcomp>
    param_name: ToolParameterDefinitionsValue(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for ToolParameterDefinitionsValue
type
  none is not an allowed value (type=type_error.none.not_allowed)
harry-cohere commented 5 months ago

Thanks for creating an issue @gopinathan-av - we'll aim to make a fix next week

harry-cohere commented 5 months ago

Sorry that it's taking a bit longer than thought to address this @gopinathan-av - aiming to have a better version this week. Thanks for your patience!

RichmondAlake commented 2 months ago

Hi @harry-cohere,

Following up on this issue, is there a fix for this?

Thanks