-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatcmpl-claude.py
More file actions
53 lines (44 loc) · 1.62 KB
/
chatcmpl-claude.py
File metadata and controls
53 lines (44 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from agents import Agent, ModelSettings, Runner, function_tool, OpenAIChatCompletionsModel
from openai import AsyncOpenAI
import os
custom_client = AsyncOpenAI(
base_url="https://api.anthropic.com/v1/",
api_key=os.getenv("ANTHROPIC_API_KEY")
)
my_model = OpenAIChatCompletionsModel(
model="claude-sonnet-4-5",
openai_client=custom_client,
)
@function_tool
def echo_tool(input: str) -> str:
print("called echo_tool")
return input
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant",
model=my_model,
tools=[echo_tool],
model_settings=ModelSettings(
extra_body={
"thinking": { "type": "enabled", "budget_tokens": 2000 }
}
)
)
result = Runner.run_sync(
agent, "Use the echo_tool to echo the message 'Hello, world!"
)
print(result.final_output)
print("------")
print(result.to_input_list())
# openai.BadRequestError: Error code: 400 - {'error':
# {'code': 'invalid_request_error', 'message':
# 'messages.1.content.0.type: Expected `thinking` or `redacted_thinking`,
# but found `tool_use`. When `thinking` is enabled, a final `assistant` message
# must start with a thinking block (preceeding the lastmost set of `tool_use` and
# `tool_result` blocks). We recommend you include thinking blocks from previous turns.
# To avoid this requirement, disable `thinking`. Please consult our
# documentation at https://docs.claude.com/en/docs/build-with-claude/extended-thinking',
# 'type': 'invalid_request_error', 'param': None}}
# 2025/12/6
# It seems official Claude endpoint does not output thinking part.
# So there is no solution.