【BUG】Does not support extracting multiple Python code blocks from response.content #3

Closed
opened 2026-02-16 07:16:28 -05:00 by yindo · 2 comments
Owner

Originally created by @HamaWhiteGG on GitHub (Mar 27, 2025).

In math_example.py, I received multi blocks of python code in response.content, for example:


I need to solve this problem step by step.

First, I'll find where the baseball lands when hit by the batter. Then, I'll calculate where the ball lands after being thrown by the outfielder. Finally, I'll determine the distance between the original batting position and the final landing position.

Let's start by finding where the ball first lands:

# Constants
g = 9.81  # acceleration due to gravity in m/s²

# Batter's hit parameters
v0_batter = 45.847  # initial velocity in m/s
angle_batter_deg = 23.474  # angle in degrees
angle_batter = radians(angle_batter_deg)  # convert to radians

# Horizontal and vertical components of initial velocity
v0x_batter = v0_batter * cos(angle_batter)
v0y_batter = v0_batter * sin(angle_batter)

# Time to reach the ground (when y = 0)
# Using the formula: y = v0y*t - (1/2)g*t²
# When y = 0: 0 = v0y*t - (1/2)g*t²
# Solving for t: t = 0 or t = 2*v0y/g
time_of_flight_batter = divide(multiply(2, v0y_batter), g)

# Horizontal distance traveled
x_distance_batter = multiply(v0x_batter, time_of_flight_batter)

print(f"The ball first lands {x_distance_batter:.2f} meters from the batter")

Now, let's calculate the trajectory of the ball thrown by the outfielder:

# Outfielder's throw parameters
v0_outfielder = 24.12  # initial velocity in m/s
angle_outfielder_deg = 39.12  # angle in degrees
angle_outfielder = radians(angle_outfielder_deg)  # convert to radians

# The outfielder is at the landing point of the batter's hit
# We need to set up coordinates where the outfielder is at the origin, 
# but throwing back toward the batter (negative x-direction)

# Horizontal and vertical components of outfielder's throw
# Note the negative sign for horizontal component because outfielder throws back toward batter
v0x_outfielder = multiply(-1, multiply(v0_outfielder, cos(angle_outfielder)))
v0y_outfielder = multiply(v0_outfielder, sin(angle_outfielder))

# Time for the ball to reach the ground again
time_of_flight_outfielder = divide(multiply(2, v0y_outfielder), g)

# Horizontal distance traveled during outfielder's throw (relative to outfielder position)
x_distance_outfielder = multiply(v0x_outfielder, time_of_flight_outfielder)

# Total horizontal distance from batter's original position
total_distance = add(x_distance_batter, x_distance_outfielder)

print(f"The ball thrown by the outfielder lands {abs(x_distance_outfielder):.2f} meters from the outfielder")
print(f"The final position of the ball is {total_distance:.2f} meters from where the batter originally hit it")

Let's see the results of these calculations to find the final distance.


but the call_model only extract the first python code. code = response.content.split("```")[1]

    def call_model(state: CodeActState) -> Command:
        messages = [{"role": "system", "content": prompt}] + state["messages"]
        response = model.invoke(messages)
        if "```" in response.content:
            # get content between fences
            code = response.content.split("```")[1]
            # remove first line, which is the language or empty string
            code = "\n".join(code.splitlines()[1:])
            return Command(goto="sandbox", update={"messages": [response], "script": code})
        else:
            # no code block, end the loop and respond to the user
            return Command(update={"messages": [response], "script": None})

Originally created by @HamaWhiteGG on GitHub (Mar 27, 2025). In math_example.py, I received multi blocks of python code in response.content, for example: ________________________________________________________________________________ I need to solve this problem step by step. First, I'll find where the baseball lands when hit by the batter. Then, I'll calculate where the ball lands after being thrown by the outfielder. Finally, I'll determine the distance between the original batting position and the final landing position. Let's start by finding where the ball first lands: ```python # Constants g = 9.81 # acceleration due to gravity in m/s² # Batter's hit parameters v0_batter = 45.847 # initial velocity in m/s angle_batter_deg = 23.474 # angle in degrees angle_batter = radians(angle_batter_deg) # convert to radians # Horizontal and vertical components of initial velocity v0x_batter = v0_batter * cos(angle_batter) v0y_batter = v0_batter * sin(angle_batter) # Time to reach the ground (when y = 0) # Using the formula: y = v0y*t - (1/2)g*t² # When y = 0: 0 = v0y*t - (1/2)g*t² # Solving for t: t = 0 or t = 2*v0y/g time_of_flight_batter = divide(multiply(2, v0y_batter), g) # Horizontal distance traveled x_distance_batter = multiply(v0x_batter, time_of_flight_batter) print(f"The ball first lands {x_distance_batter:.2f} meters from the batter") ``` Now, let's calculate the trajectory of the ball thrown by the outfielder: ```python # Outfielder's throw parameters v0_outfielder = 24.12 # initial velocity in m/s angle_outfielder_deg = 39.12 # angle in degrees angle_outfielder = radians(angle_outfielder_deg) # convert to radians # The outfielder is at the landing point of the batter's hit # We need to set up coordinates where the outfielder is at the origin, # but throwing back toward the batter (negative x-direction) # Horizontal and vertical components of outfielder's throw # Note the negative sign for horizontal component because outfielder throws back toward batter v0x_outfielder = multiply(-1, multiply(v0_outfielder, cos(angle_outfielder))) v0y_outfielder = multiply(v0_outfielder, sin(angle_outfielder)) # Time for the ball to reach the ground again time_of_flight_outfielder = divide(multiply(2, v0y_outfielder), g) # Horizontal distance traveled during outfielder's throw (relative to outfielder position) x_distance_outfielder = multiply(v0x_outfielder, time_of_flight_outfielder) # Total horizontal distance from batter's original position total_distance = add(x_distance_batter, x_distance_outfielder) print(f"The ball thrown by the outfielder lands {abs(x_distance_outfielder):.2f} meters from the outfielder") print(f"The final position of the ball is {total_distance:.2f} meters from where the batter originally hit it") ``` Let's see the results of these calculations to find the final distance. ________________________________________________________________________________ but the call_model only extract the first python code. `code = response.content.split("```")[1]` ```python def call_model(state: CodeActState) -> Command: messages = [{"role": "system", "content": prompt}] + state["messages"] response = model.invoke(messages) if "```" in response.content: # get content between fences code = response.content.split("```")[1] # remove first line, which is the language or empty string code = "\n".join(code.splitlines()[1:]) return Command(goto="sandbox", update={"messages": [response], "script": code}) else: # no code block, end the loop and respond to the user return Command(update={"messages": [response], "script": None}) ```
yindo added the bug label 2026-02-16 07:16:28 -05:00
yindo closed this issue 2026-02-16 07:16:28 -05:00
Author
Owner

@vbarda commented on GitHub (Mar 31, 2025):

@HamaWhiteGG thanks for reporting - will add support for detecting multiple code blocks

@vbarda commented on GitHub (Mar 31, 2025): @HamaWhiteGG thanks for reporting - will add support for detecting multiple code blocks
Author
Owner

@vbarda commented on GitHub (Apr 5, 2025):

Fixed in 0.1.1

@vbarda commented on GitHub (Apr 5, 2025): Fixed in 0.1.1
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph-codeact#3