Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/callbacks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ defmodule LangChain.Callbacks do
msg =
"Callback handler for #{inspect(callback_name)} raised an exception: #{LangChainError.format_exception(err, __STACKTRACE__, :short)}"

Logger.error(msg)
raise LangChainError, msg
end

other ->
msg =
"Unexpected callback handler. Callback #{inspect(callback_name)} was assigned #{inspect(other)}"

Logger.error(msg)
raise LangChainError, msg
end
end)
Expand Down
18 changes: 2 additions & 16 deletions lib/chains/llm_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,7 @@ defmodule LangChain.Chains.LLMChain do
end
end
rescue
err ->
# Log the error and stack trace, then try again.
Logger.error(
"Rescued from exception during with_fallback processing. Error: #{inspect(err)}\nStack trace:\n#{Exception.format(:error, err, __STACKTRACE__)}"
)

_err ->
try_chain_with_llm(use_chain, tail, before_fallback_fn, run_fn)
end
end
Expand Down Expand Up @@ -948,12 +943,10 @@ defmodule LangChain.Chains.LLMChain do

{:error, %LangChainError{} = reason} ->
if chain.verbose, do: IO.inspect(reason, label: "ERROR")
Logger.error("Error during chat call. Reason: #{inspect(reason)}")
{:error, chain, reason}

{:error, string_reason} when is_binary(string_reason) ->
if chain.verbose, do: IO.inspect(string_reason, label: "ERROR")
Logger.error("Error during chat call. Reason: #{inspect(string_reason)}")
{:error, chain, LangChainError.exception(message: string_reason)}
end
end
Expand Down Expand Up @@ -1112,8 +1105,6 @@ defmodule LangChain.Chains.LLMChain do
end
rescue
err ->
Logger.error("Exception raised in processor #{inspect(proc)}")

{:halt,
{:halted,
Message.new_user!("ERROR: An exception was raised! Exception: #{inspect(err)}")}}
Expand Down Expand Up @@ -1386,10 +1377,6 @@ defmodule LangChain.Chains.LLMChain do
end
rescue
err ->
Logger.error(
"Function #{function.name} failed in execution. Exception: #{LangChainError.format_exception(err, __STACKTRACE__)}"
)

ToolResult.new!(%{
tool_call_id: call.call_id,
content: "ERROR executing tool: #{inspect(err)}",
Expand All @@ -1414,8 +1401,7 @@ defmodule LangChain.Chains.LLMChain do
message = %Message{message | status: message_status}
add_message(updated_chain, message)

{:error, reason} ->
Logger.error("Error attempting to cancel_delta. Reason: #{inspect(reason)}")
{:error, _reason} ->
chain
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/chains/summarize_conversation_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ defmodule LangChain.Chains.SummarizeConversationChain do
# and assistant messages, keep the # of messages to keep.
splice_messages_with_summary(summarizer, to_summarize, summary)

{:error, _summarizer_chain, reason} ->
Logger.error(
"SummarizeConversationChain failed. Reason: #{inspect(reason)}. Returning original chain"
)

{:error, _summarizer_chain, _reason} ->
to_summarize
end
else
Expand Down
3 changes: 1 addition & 2 deletions lib/chains/text_to_title_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ You expertly summarize the User Text into a short title or phrase to represent a
if chain.verbose, do: IO.inspect(title, label: "TITLE GENERATED")
title

{:error, _chain, reason} ->
Logger.error("TextToTitleChain failed. Reason: #{inspect(reason)}. Returning DEFAULT")
{:error, _chain, _reason} ->
chain.fallback_title
end
end
Expand Down
24 changes: 5 additions & 19 deletions lib/chat_models/chat_anthropic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,7 @@ defmodule LangChain.ChatModels.ChatAnthropic do
error

other ->
message = "Unexpected and unhandled API response! #{inspect(other)}"
Logger.error(message)
{:error, LangChainError.exception(type: "unexpected_response", message: message)}
{:error, LangChainError.exception(type: "unexpected_response", message: "Unexpected and unhandled API response! #{inspect(other)}")}
end
end

Expand Down Expand Up @@ -890,9 +888,7 @@ defmodule LangChain.ChatModels.ChatAnthropic do
error

other ->
message = "Unhandled and unexpected response from streamed post call. #{inspect(other)}"
Logger.error(message)
{:error, LangChainError.exception(type: "unexpected_response", message: message)}
{:error, LangChainError.exception(type: "unexpected_response", message: "Unhandled and unexpected response from streamed post call. #{inspect(other)}")}
end
end

Expand Down Expand Up @@ -1152,18 +1148,15 @@ defmodule LangChain.ChatModels.ChatAnthropic do
"error" => %{"type" => type, "message" => reason}
} = response
) do
Logger.error("Received error from API: #{inspect(response)}")
{:error, LangChainError.exception(type: type, message: reason, original: response)}
end

def do_process_response(_model, %{"error" => %{"message" => reason} = error} = response) do
Logger.error("Received error from API: #{inspect(response)}")
{:error, LangChainError.exception(type: error["type"], message: reason, original: response)}
end

def do_process_response(_model, {:error, %Jason.DecodeError{} = response}) do
error_message = "Received invalid JSON: #{inspect(response)}"
Logger.error(error_message)

{:error,
LangChainError.exception(type: "invalid_json", message: error_message, original: response)}
Expand Down Expand Up @@ -1202,8 +1195,6 @@ defmodule LangChain.ChatModels.ChatAnthropic do
end

def do_process_response(_model, other) do
Logger.error("Failed to process an unexpected response. #{inspect(other)}")

{:error,
LangChainError.exception(
type: "unexpected_response",
Expand Down Expand Up @@ -1393,8 +1384,7 @@ defmodule LangChain.ChatModels.ChatAnthropic do
def relevant_event?(%{"type" => "content_block_stop"}), do: false
def relevant_event?(%{"type" => "message_stop"}), do: false
# catch-all for when we miss something
def relevant_event?(event) do
Logger.error("Unsupported event received when parsing Anthropic response: #{inspect(event)}")
def relevant_event?(_event) do
false
end

Expand Down Expand Up @@ -1695,9 +1685,7 @@ defmodule LangChain.ChatModels.ChatAnthropic do
value

other ->
message = "Received unsupported media type for ContentPart: #{inspect(other)}"
Logger.error(message)
raise LangChainError, message
raise LangChainError, "Received unsupported media type for ContentPart: #{inspect(other)}"
end

%{
Expand All @@ -1720,9 +1708,7 @@ defmodule LangChain.ChatModels.ChatAnthropic do
value

other ->
message = "Received unsupported media type for ContentPart: #{inspect(other)}"
Logger.error(message)
raise LangChainError, message
raise LangChainError, "Received unsupported media type for ContentPart: #{inspect(other)}"
end

%{
Expand Down
14 changes: 0 additions & 14 deletions lib/chat_models/chat_bumblebee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do

{:error, changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, reason}
end

Expand All @@ -375,7 +374,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do

{:error, changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, reason}
end
end
Expand Down Expand Up @@ -412,7 +410,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do

{:error, changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, reason}
end

Expand All @@ -426,7 +423,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do

{:error, changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, reason}
end
end
Expand Down Expand Up @@ -460,7 +456,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do

{:error, changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, reason}
end

Expand All @@ -474,7 +469,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do

{:error, changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, reason}
end
end
Expand Down Expand Up @@ -505,8 +499,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do
[message]

{:error, %Ecto.Changeset{} = changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to create non-streamed full message: #{inspect(reason)}")
{:error, LangChainError.exception(changeset)}
end
end
Expand Down Expand Up @@ -549,12 +541,6 @@ defmodule LangChain.ChatModels.ChatBumblebee do
delta

{:error, %Ecto.Changeset{} = changeset} ->
reason = Utils.changeset_error_to_string(changeset)

Logger.error(
"Failed to process received model's MessageDelta data: #{inspect(reason)}"
)

raise LangChainError.exception(changeset)
end
end
Expand Down
16 changes: 1 addition & 15 deletions lib/chat_models/chat_deepseek.ex
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ defmodule LangChain.ChatModels.ChatDeepSeek do
end

other ->
Logger.error("Unexpected and unhandled API response! #{inspect(other)}")
other
end
end
Expand Down Expand Up @@ -760,11 +759,7 @@ defmodule LangChain.ChatModels.ChatDeepSeek do
)}
end

other ->
Logger.error(
"Unhandled and unexpected response from streamed post call. #{inspect(other)}"
)

_other ->
{:error,
LangChainError.exception(type: "unexpected_response", message: "Unexpected response")}
end
Expand Down Expand Up @@ -819,7 +814,6 @@ defmodule LangChain.ChatModels.ChatDeepSeek do
end

defp parse_combined_data(_incomplete, _json, done, depth) when depth >= 10 do
Logger.error("Stream parsing recursion limit exceeded: depth = #{depth}")
{done, ""}
end

Expand Down Expand Up @@ -956,8 +950,6 @@ defmodule LangChain.ChatModels.ChatDeepSeek do
call

{:error, %Ecto.Changeset{} = changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to process ToolCall for a function. Reason: #{reason}")
{:error, LangChainError.exception(changeset)}
end
end
Expand Down Expand Up @@ -1000,8 +992,6 @@ defmodule LangChain.ChatModels.ChatDeepSeek do
call

{:error, %Ecto.Changeset{} = changeset} ->
reason = Utils.changeset_error_to_string(changeset)
Logger.error("Failed to process ToolCall for a function. Reason: #{reason}")
{:error, LangChainError.exception(changeset)}
end
end
Expand Down Expand Up @@ -1057,25 +1047,21 @@ defmodule LangChain.ChatModels.ChatDeepSeek do
_other -> nil
end

Logger.error("Received error from API: #{inspect(reason)}")
{:error, LangChainError.exception(type: type, message: reason, original: response)}
end

def do_process_response(_model, %{"error" => %{"message" => reason}} = response) do
Logger.error("Received error from API: #{inspect(reason)}")
{:error, LangChainError.exception(message: reason, original: response)}
end

def do_process_response(_model, {:error, %Jason.DecodeError{} = response}) do
error_message = "Received invalid JSON: #{inspect(response)}"
Logger.error(error_message)

{:error,
LangChainError.exception(type: "invalid_json", message: error_message, original: response)}
end

def do_process_response(_model, other) do
Logger.error("Trying to process an unexpected response. #{inspect(other)}")
{:error, LangChainError.exception(message: "Unexpected response", original: other)}
end

Expand Down
14 changes: 2 additions & 12 deletions lib/chat_models/chat_google_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ defmodule LangChain.ChatModels.ChatGoogleAI do
"image/type"

other ->
message = "Received unsupported media type for ContentPart: #{inspect(other)}"
Logger.error(message)
raise LangChainError, message
raise LangChainError, "Received unsupported media type for ContentPart: #{inspect(other)}"
end

%{
Expand Down Expand Up @@ -561,7 +559,6 @@ defmodule LangChain.ChatModels.ChatGoogleAI do
LangChainError.exception(type: "timeout", message: "Request timed out", original: err)}

other ->
Logger.error("Unexpected and unhandled API response! #{inspect(other)}")
other
end
end
Expand Down Expand Up @@ -625,11 +622,7 @@ defmodule LangChain.ChatModels.ChatGoogleAI do
{:error,
LangChainError.exception(type: "timeout", message: "Request timed out", original: err)}

other ->
Logger.error(
"Unhandled and unexpected response from streamed post call. #{inspect(other)}"
)

_other ->
{:error,
LangChainError.exception(type: "unexpected_response", message: "Unexpected response")}
end
Expand Down Expand Up @@ -797,20 +790,17 @@ defmodule LangChain.ChatModels.ChatGoogleAI do
end

def do_process_response(_model, %{"error" => %{"message" => reason}} = response, _) do
Logger.error("Received error from API: #{inspect(reason)}")
{:error, LangChainError.exception(message: reason, original: response)}
end

def do_process_response(_model, {:error, %Jason.DecodeError{} = response}, _) do
error_message = "Received invalid JSON: #{inspect(response)}"
Logger.error(error_message)

{:error,
LangChainError.exception(type: "invalid_json", message: error_message, original: response)}
end

def do_process_response(_model, other, _) do
Logger.error("Trying to process an unexpected response. #{inspect(other)}")

{:error,
LangChainError.exception(
Expand Down
Loading