1from textwrap import dedent
2
3from kern.agent import Agent
4from kern.models.openai import OpenAIResponses
5from kern.tools.reasoning import ReasoningTools
6
7"""Test function to verify reasoning_content is populated in RunOutput."""
8print("\n=== Testing reasoning_content generation ===\n")
9
10# Create an agent with ReasoningTools
11agent = Agent(
12 model=OpenAIResponses(id="gpt-5.2"),
13 tools=[ReasoningTools(add_instructions=True)],
14 instructions=dedent("""\
15 You are an expert problem-solving assistant with strong analytical skills! ��
16 Use step-by-step reasoning to solve the problem.
17 \
18 """),
19)
20
21# Test 1: Non-streaming mode
22print("Running with stream=False...")
23response = agent.run("What is the sum of the first 10 natural numbers?", stream=False)
24
25# Check reasoning_content
26if hasattr(response, "reasoning_content") and response.reasoning_content:
27 print("✅ reasoning_content FOUND in non-streaming response")
28 print(f" Length: {len(response.reasoning_content)} characters")
29 print("\n=== reasoning_content preview (non-streaming) ===")
30 preview = response.reasoning_content[:1000]
31 if len(response.reasoning_content) > 1000:
32 preview += "..."
33 print(preview)
34else:
35 print("❌ reasoning_content NOT FOUND in non-streaming response")
36
37# Process streaming responses to find the final one
38print("\n\n=== Test 2: Processing stream to find final response ===\n")
39
40# Create another fresh agent
41streaming_agent_alt = Agent(
42 model=OpenAIResponses(id="gpt-5.2"),
43 tools=[ReasoningTools(add_instructions=True)],
44 instructions=dedent("""\
45 You are an expert problem-solving assistant with strong analytical skills! ��
46 Use step-by-step reasoning to solve the problem.
47 \
48 """),
49)
50
51# Process streaming responses and look for the final RunOutput
52final_response = None
53for event in streaming_agent_alt.run(
54 "What is the value of 3! (factorial)?",
55 stream=True,
56 stream_events=True,
57):
58 # The final event in the stream should be a RunOutput object
59 if hasattr(event, "reasoning_content"):
60 final_response = event
61
62print("--- Checking reasoning_content from final stream event ---")
63if (
64 final_response
65 and hasattr(final_response, "reasoning_content")
66 and final_response.reasoning_content
67):
68 print("✅ reasoning_content FOUND in final stream event")
69 print(f" Length: {len(final_response.reasoning_content)} characters")
70 print("\n=== reasoning_content preview (final stream event) ===")
71 preview = final_response.reasoning_content[:1000]
72 if len(final_response.reasoning_content) > 1000:
73 preview += "..."
74 print(preview)
75else:
76 print("❌ reasoning_content NOT FOUND in final stream event")