langgraph_rb 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 071bb22811337400b9569f90e07b50c0f7c6784b1025ce30b0502c5d321965b5
4
- data.tar.gz: dad734ef3e60f76d8bf47de4c1f5f50a1b361dad8cfd7e36ed57e3cd4f998dca
3
+ metadata.gz: bd46b52a6c57bec611953702facad328eda63e0c5f66f85c403230d05548709b
4
+ data.tar.gz: 19e32e26b6c38044536b8498c7900bcb19386adf1853881cc3a9c3ca3861dae4
5
5
  SHA512:
6
- metadata.gz: 20c5c1c2ab980770d8ef0ca69159c1566c889c3ecd80d9185f6767b7954d92f5a15847ac50b1024dbd976670dc90d76b1bec9a95fa5d55b6123d6cdb26633bd2
7
- data.tar.gz: a6edae4423708c7f807a050a9d4238d36ed188d0a0697e04eb2f21cebb3987ddf9d7f627cd6657d0b128470a58ca2a6895d02614da30341c73dbd5af1584337e
6
+ metadata.gz: ed37faa9f9cc70f30a63ac31f96daafd2fa8f6362f78607ac3532ee396fdd04037265a428fc2e5492b4971100be836845e29228e59b5876abc8b50239c9b83a4
7
+ data.tar.gz: 33a4b49fa73f482c4a66299b0a58964be9a81f505e44d35c36b033334b73a24341fc3e78349ff0652f26ca0ae6884b5623fa07d8263570367a790b1324e278ec
@@ -38,7 +38,7 @@ end
38
38
  def run_chat_openai_tools
39
39
  tools = [MovieInfoTool.new(api_key: ENV['TMDB_API_KEY'] || 'demo')]
40
40
 
41
- chat = LangGraphRB::ChatOpenAI.new(model: ENV.fetch('OPENAI_MODEL', 'gpt-4o-mini'), temperature: 0)
41
+ chat = LangGraphRB::ChatOpenAI.new(model: ENV.fetch('OPENAI_MODEL', 'gpt-4o-min3i'), temperature: 0)
42
42
  chat = chat.bind_tools(tools)
43
43
 
44
44
  observers = [LangGraphRB::Observers::LangfuseObserver.new(name: 'chat-openai-tools-example')]
@@ -50,7 +50,11 @@ def run_chat_openai_tools
50
50
  { messages: existing + [user_msg] }
51
51
  end
52
52
 
53
- llm_node :chat, llm_client: chat, system_prompt: "You are a movie assistant. Use tools when helpful."
53
+ # Optional: callback to observe each assistant message as it's produced
54
+ add_message_callback = ->(message) { puts "New message: #{message}" }
55
+ sys_prompt = "You are a movie assistant. Use tools when helpful."
56
+
57
+ llm_node :chat, llm_client: chat, system_prompt: sys_prompt, add_message_callback: add_message_callback
54
58
 
55
59
  tool_node :tool, tools: tools
56
60
 
@@ -77,8 +77,14 @@ module LangGraphRB
77
77
  else
78
78
  text_content
79
79
  end
80
+ rescue => e
81
+ notify_llm_error({
82
+ error: e.message
83
+ })
84
+ raise e
80
85
  end
81
86
 
87
+
82
88
  private
83
89
 
84
90
  def normalize_messages(messages)
@@ -35,11 +35,11 @@ module LangGraphRB
35
35
  end
36
36
  end
37
37
 
38
- def llm_node(name, llm_client:, system_prompt: nil, &block)
38
+ def llm_node(name, llm_client:, system_prompt: nil, add_message_callback: nil, &block)
39
39
  name = name.to_sym
40
40
  raise GraphError, "Node '#{name}' already exists" if @nodes.key?(name)
41
41
 
42
- @nodes[name] = LLMNode.new(name, llm_client: llm_client, system_prompt: system_prompt, &block)
42
+ @nodes[name] = LLMNode.new(name, llm_client: llm_client, system_prompt: system_prompt, add_message_callback: add_message_callback, &block)
43
43
  end
44
44
 
45
45
  def tool_node(name, tools:, &block)
@@ -54,6 +54,16 @@ module LangGraphRB
54
54
  end
55
55
  end
56
56
  end
57
+
58
+ def notify_llm_error(payload)
59
+ @observers.each do |observer|
60
+ begin
61
+ observer.on_llm_error(payload, @node_name)
62
+ rescue => _e
63
+ # Ignore observer errors
64
+ end
65
+ end
66
+ end
57
67
  end
58
68
  end
59
69
 
@@ -35,11 +35,12 @@ module LangGraphRB
35
35
 
36
36
  # Specialized node for LLM calls
37
37
  class LLMNode < Node
38
- attr_reader :llm_client, :system_prompt
38
+ attr_reader :llm_client, :system_prompt, :add_message_callback
39
39
 
40
- def initialize(name, llm_client:, system_prompt: nil, &block)
40
+ def initialize(name, llm_client:, system_prompt: nil, add_message_callback: nil, &block)
41
41
  @llm_client = llm_client
42
42
  @system_prompt = system_prompt
43
+ @add_message_callback = add_message_callback
43
44
 
44
45
  # Use default LLM behavior if no custom block provided
45
46
  super(name, &(block || method(:default_llm_call)))
@@ -49,7 +50,8 @@ module LangGraphRB
49
50
  # Auto-inject LLM config into the context for both default and custom blocks
50
51
  merged_context = (context || {}).merge(
51
52
  llm_client: @llm_client,
52
- system_prompt: @system_prompt
53
+ system_prompt: @system_prompt,
54
+ add_message_callback: @add_message_callback
53
55
  )
54
56
 
55
57
  begin
@@ -87,12 +89,14 @@ module LangGraphRB
87
89
  content: nil,
88
90
  tool_calls: response[:tool_calls]
89
91
  }
92
+ @add_message_callback&.call(assistant_msg)
90
93
  {
91
94
  messages: (state[:messages] || []) + [assistant_msg],
92
95
  tool_call: response[:tool_calls].first
93
96
  }
94
97
  else
95
98
  assistant_msg = { role: 'assistant', content: response.to_s }
99
+ @add_message_callback&.call(assistant_msg)
96
100
  {
97
101
  messages: (state[:messages] || []) + [assistant_msg],
98
102
  last_response: response.to_s
@@ -70,6 +70,10 @@ module LangGraphRB
70
70
  # Override in subclasses
71
71
  end
72
72
 
73
+ def on_llm_error(event)
74
+ # Override in subclasses
75
+ end
76
+
73
77
  protected
74
78
 
75
79
  # Helper method to create standardized event structure
@@ -165,6 +165,24 @@ module LangGraphRB
165
165
  rescue => _e
166
166
  end
167
167
 
168
+ def on_llm_error(data, node_name)
169
+ record = with_records_lock do
170
+ stack = @records_by_node[node_name]
171
+ stack.empty? ? nil : stack[-1]
172
+ end
173
+ return unless record && record[:generation]
174
+
175
+ generation = record[:generation]
176
+ generation.output = data[:error]
177
+ generation.end_time = Time.now.utc
178
+ Langfuse.update_generation(generation)
179
+
180
+ with_records_lock do
181
+ record[:generation] = nil
182
+ end
183
+ rescue => _e
184
+ end
185
+
168
186
  private
169
187
 
170
188
  def ensure_trace!(event)
@@ -1,3 +1,3 @@
1
1
  module LangGraphRB
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: langgraph_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Toro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-09-20 00:00:00.000000000 Z
11
+ date: 2025-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json