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

OpenAI Agent returning structured output has type errors #3774

Closed
garrettg123 opened this issue Dec 23, 2023 · 2 comments · Fixed by #3770
Closed

OpenAI Agent returning structured output has type errors #3774

garrettg123 opened this issue Dec 23, 2023 · 2 comments · Fixed by #3770
Assignees
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@garrettg123
Copy link

garrettg123 commented Dec 23, 2023

Following the exact guide at https://js.langchain.com/docs/modules/agents/how_to/agent_structured, these errors come up:

Type 'Function' is not assignable to type 'FunctionDefinition'.
  Types of property 'parameters' are incompatible.
    Type 'FunctionParameters | undefined' is not assignable to type 'Record<string, unknown>'.
      Type 'undefined' is not assignable to type 'Record<string, unknown>'.ts(2322)
Type '{ readonly name: "response"; readonly description: "Return the response to the user"; readonly parameters: JsonSchema7Type & { $schema?: string | undefined; definitions?: { ...; } | undefined; }; } | Function' is not assignable to type 'FunctionDefinition'.
  Type 'Function' is not assignable to type 'FunctionDefinition'.ts(2322)

for

  const llmWithTools = llm.bind({
    functions: [
      ...tools.map(tool => formatToOpenAIFunction(tool)),
      responseOpenAIFunction,
    ],
  })

and then

Type '(i: { input: string; steps: Array<AgentStep>; }) => BaseMessage[]' is not assignable to type 'RunnableLike<{ input: string; }, any>'.
  Type '(i: { input: string; steps: Array<AgentStep>; }) => BaseMessage[]' is not assignable to type 'RunnableFunc<{ input: string; }, any>'.
    Types of parameters 'i' and 'input' are incompatible.
      Property 'steps' is missing in type '{ input: string; }' but required in type '{ input: string; steps: AgentStep[]; }'.ts(2322)
task-context.ts(163, 46): 'steps' is declared here.

for

  const runnableAgent = RunnableSequence.from([
    {
      input: (i: { input: string }) => i.input,
      agent_scratchpad: (i: { input: string; steps: Array<AgentStep> }) =>
        formatForOpenAIFunctions(i.steps),
    },
    prompt,
    llmWithTools,
    structuredOutputParser,
  ])
Copy link

dosubot bot commented Dec 23, 2023

🤖

Hey there, @garrettg123! Good to see you around again. Let's dive into this challenge together, shall we?

It seems like you're encountering type errors due to a mismatch between the expected types and the ones you're providing. Let's try to address these issues one by one.

  1. For the first two errors, it seems like the issue is with the FunctionDefinition type. In LangChainJS, a FunctionDefinition is expected to have a specific structure. The parameters property of a FunctionDefinition is expected to be a Record<string, unknown>, not FunctionParameters | undefined. This means that you need to ensure that every function you're providing in the functions array of llm.bind has a parameters property that is an object with string keys and values of any type. Here's an example of how you might define a function:
const myFunction: FunctionDefinition = {
  name: 'myFunction',
  description: 'My function',
  parameters: {
    param1: 'value1',
    param2: 'value2',
    // ...
  },
  // ...
};

Then, you can use this function in the functions array:

const llmWithTools = llm.bind({
  functions: [
    ...tools.map(tool => formatToOpenAIFunction(tool)),
    responseOpenAIFunction,
    myFunction,
  ],
});
  1. For the third error, it seems like the issue is with the RunnableLike type. In LangChainJS, a RunnableLike is expected to be a function that takes an object with a specific structure as its input. In your case, the input object is expected to have an input property of type string and a steps property of type Array<AgentStep>. However, in the input function of your RunnableSequence, you're only providing an object with an input property. To fix this error, you need to provide an object with both input and steps properties in the input function. Here's how you might do it:
const runnableAgent = RunnableSequence.from([
  {
    input: (i: { input: string; steps: Array<AgentStep> }) => i,
    agent_scratchpad: (i: { input: string; steps: Array<AgentStep> }) =>
      formatForOpenAIFunctions(i.steps),
  },
  prompt,
  llmWithTools,
  structuredOutputParser,
]);

I hope this helps! If you're still encountering issues, could you please provide more information about the formatToOpenAIFunction, responseOpenAIFunction, and formatForOpenAIFunctions functions, as well as the tools array and the AgentStep type? This would help me understand your code better and provide a more accurate solution.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Dec 23, 2023
@jacoblee93
Copy link
Collaborator

Thanks for the heads up, am looking into it now

CC @bracesproul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants