I am having trouble using these two functionalities together (in the same agent). The goal is to allow the agent access to some tools, and then have the final response follow a defined Structured Output (OpenAI). When I use the agent with only one of the features, it works as intended, but when combining them, the response format defined makes it impossible for the LLM to match the required inputs for the tools.
Example of successful usage only using one component at a time:
llm_with_tools = self.llm.bind_tools(
tools=tools,
parallel_tool_calls=self.parallel_tool_calls,
)
## OR
llm_with_struct_output = self.llm.with_structured_output(
schema=InternalResult
)
Example of simultaneous usage (both functionalities in the same agent):
unified_model = self.llm.with_structured_output(
InternalResult,
method="json_schema",
include_raw=True,
strict=True,
tools=tools
)
## OR
llm_with_tools = self.llm.bind_tools(
tools=tools,
parallel_tool_calls=self.parallel_tool_calls,
).with_structured_output(schema=InternalResult)
These last examples always produce the following error, due to a mismatch between the tool’s required input and LLM output:
Error: Error code: 400 - {‘error’: {‘message’: \“Invalid schema for function ‘get_company_news’: In context=(), ‘additionalProperties’ is required to be supplied and to be false.\”, ‘type’: ‘invalid_request_error’}}
Currently, the workaround I am using is doing 2 different LLM calls (one for tool calling, another for formatting) but this adds a lot of time and cost to the process. Another possisbility is to have the Structured Output schema as a tool and guide the agent to always call it, but this approach is not as robust as I wished.
Is there any other way to create an agent with these two functionalities?
Thank you!