Skip to content

Commit 29c8af2

Browse files
fix: resolve FINAL_VAR to actual variable value instead of returning tuple
The find_final_answer() function returns a tuple ("FINAL_VAR", "var_name"), but the caller was passing this tuple directly as the final answer instead of resolving the variable from environment.locals. - Refactor check_for_final_answer() to take env_locals dict directly - Remove broken logger.log_tool_execution() dependency (method didn't exist) - Use check_for_final_answer() in rlm.py to properly resolve FINAL_VAR
1 parent 455333a commit 29c8af2

2 files changed

Lines changed: 19 additions & 22 deletions

File tree

‎rlm/core/rlm.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from rlm.environments import BaseEnv, get_environment
1717
from rlm.logger import RLMLogger, VerbosePrinter
1818
from rlm.utils.parsing import (
19+
check_for_final_answer,
1920
find_code_blocks,
20-
find_final_answer,
2121
format_iteration,
2222
)
2323
from rlm.utils.prompts import (
@@ -186,7 +186,7 @@ def completion(
186186
)
187187

188188
# Check if RLM is done and has a final answer.
189-
final_answer = find_final_answer(iteration.response)
189+
final_answer = check_for_final_answer(iteration.response, environment.locals)
190190
iteration.final_answer = final_answer
191191

192192
# If logger is used, log the iteration.

‎rlm/utils/parsing.py‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,16 @@ def format_execution_result(result: REPLResult) -> str:
115115
return "\n\n".join(result_parts) if result_parts else "No output"
116116

117117

118-
def check_for_final_answer(response: str, repl_env, logger) -> str | None:
119-
"""Check if response contains a final answer."""
118+
def check_for_final_answer(response: str, env_locals: dict) -> str | None:
119+
"""Check if response contains a final answer and resolve it.
120+
121+
Args:
122+
response: The LLM response text to check for FINAL/FINAL_VAR patterns.
123+
env_locals: Dictionary of local variables from the REPL environment.
124+
125+
Returns:
126+
The resolved final answer string, or None if not found.
127+
"""
120128
result = find_final_answer(response)
121129
if result is None:
122130
return None
@@ -126,24 +134,13 @@ def check_for_final_answer(response: str, repl_env, logger) -> str | None:
126134
if answer_type == "FINAL":
127135
return content
128136
elif answer_type == "FINAL_VAR":
129-
# Get the variable directly from the REPL environment
130-
try:
131-
# Strip spaces, quotes, and newlines from variable name
132-
variable_name = content.strip().strip('"').strip("'").strip("\n").strip("\r")
133-
134-
# Check if variable exists in the REPL environment's locals
135-
if variable_name in repl_env.locals:
136-
variable_value = repl_env.locals[variable_name]
137-
return str(variable_value)
138-
else:
139-
error_msg = f"Variable '{variable_name}' not found in REPL environment"
140-
logger.log_tool_execution("FINAL_VAR", error_msg)
141-
return None
142-
except Exception as e:
143-
error_msg = f"Error retrieving variable '{variable_name}': {str(e)}"
144-
print("ERROR MESSAGE", error_msg)
145-
logger.log_tool_execution("FINAL_VAR", error_msg)
146-
return None
137+
# Strip spaces, quotes, and newlines from variable name
138+
variable_name = content.strip().strip('"').strip("'").strip("\n").strip("\r")
139+
140+
# Check if variable exists in the environment's locals
141+
if variable_name in env_locals:
142+
return str(env_locals[variable_name])
143+
return None
147144

148145
return None
149146

0 commit comments

Comments
 (0)