[X] I added a very descriptive title to this issue.
[X] I searched the LangChain documentation with the integrated search.
[X] I used the GitHub search to find a similar question and didn't find it.
[X] I am sure that this is a bug in LangChain rather than my code.
[X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
from datetime import date
import requests
from langchain_community.utilities import SerpAPIWrapper
from langchain_core.output_parsers import StrOutputParser
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import create_openai_functions_agent
from langchain.agents import AgentExecutor
serpapi_api_key = "xxxxxxxxxx"
api_key = "sk-xxxxxxxxx"
api_url = "https://ai-yyds.com/v1"
llm = ChatOpenAI(base_url=api_url, api_key=api_key, model_name="gpt-4")
prompt = hub.pull("hwchase17/openai-functions-agent")
print(prompt.messages)
@tool
def search(text: str):
"""This tool is only used when real-time information needs to be searched. The search returns only the first 3 items"""
serp = SerpAPIWrapper(serpapi_api_key=serpapi_api_key)
response = serp.run(text)
print(type(response))
content = ""
if type(response) is list:
for item in response:
content += str(item["title"]) + "\n"
else:
content = response
return content
@tool
def time() -> str:
"""Return today's date and use it for any questions related to today's date.
The input should always be an empty string, and this function will always return today's date. Any mathematical operation on a date should occur outside of this function"""
return str(date.today())
@tool
def weather(city: str):
"""When you need to check the weather, you can use this tool, which returns the weather conditions for the day, tomorrow, and the day after tomorrow"""
url = "https://api.seniverse.com/v3/weather/daily.json?key=SrlXSW6OX9PssfOJ1&location=beijing&language=zh-Hans&unit=c&start=0"
response = requests.get(url)
data = response.json()
if not data or len(data['results']) == 0:
return None
daily = data['results'][0]["daily"]
content = ""
res = []
for day in daily:
info = {"city": city, "date": day["date"], "info": day["text_day"], "temperature_high": day["high"],
"temperature_low": day["low"]}
content += f"{city} date:{day['date']} info:{day['text_day']} maximum temperature:{day['high']} minimum temperature:{day['low']}\n"
res.append(info)
return content
tools = [time, weather, search]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
response1 = agent_executor.invoke({"input": "What's the weather like in Shanghai tomorrow"})
print(response1)
Error Message and Stack Trace (if applicable)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/chains/base.py", line 156, in invoke
self._call(inputs, run_manager=run_manager)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1636, in _call
next_step_output = self._take_next_step(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1342, in _take_next_step
[
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1342, in <listcomp>
[
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1370, in _iter_next_step
output = self.agent.plan(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 463, in plan
for chunk in self.runnable.stream(inputs, config={"callbacks": callbacks}):
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 3251, in stream
yield from self.transform(iter([input]), config, **kwargs)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 3238, in transform
yield from self._transform_stream_with_config(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 2052, in _transform_stream_with_config
chunk: Output = context.run(next, iterator) # type: ignore
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 3200, in _transform
for output in final_pipeline:
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 1270, in transform
for ichunk in input:
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 5262, in transform
yield from self.bound.transform(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 1288, in transform
yield from self.stream(final, config, **kwargs)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/language_models/chat_models.py", line 360, in stream
raise e
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/language_models/chat_models.py", line 340, in stream
for chunk in self._stream(messages, stop=stop, **kwargs):
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_openai/chat_models/base.py", line 520, in _stream
response = self.client.create(**payload)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_utils/_utils.py", line 277, in wrapper
return func(*args, **kwargs)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/resources/chat/completions.py", line 643, in create
return self._post(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_base_client.py", line 1266, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_base_client.py", line 942, in request
return self._request(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_base_client.py", line 1046, in _request
raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value for 'content': expected a string, got null. (request id: 20240723144941715017377sn10oSMg) (request id: 2024072306494157956522013257597)", 'type': 'invalid_request_error', 'param': 'messages.[2].content', 'code': None}}
Description
Execute the above code, sometimes it returns normally, sometimes it reports an error
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value for 'content': expected a string, got null. (request id: 20240723111146966761056DQSQiv7T) (request id: 2024072303114683478387128512399)", 'type': 'invalid_request_error', 'param': 'messages.[2].content', 'code': None}}
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
Execute the above code, sometimes it returns normally, sometimes it reports an error
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value for 'content': expected a string, got null. (request id: 20240723111146966761056DQSQiv7T) (request id: 2024072303114683478387128512399)", 'type': 'invalid_request_error', 'param': 'messages.[2].content', 'code': None}}
System Info
platform: Mac python: 3.8