Skip to content

Commit ec9c5ab

Browse files
nunombispoacsanyclaudebzaczynski
authored
added materials for How to Use the Claude API in Python for AI-Powere… (#750)
* added materials for How to Use the Claude API in Python for AI-Powered Applications * Updated code after first TR * Apply ruff format to fix Linux314 CI Pure formatter changes; no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Add README.md Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Final QA --------- Co-authored-by: Philipp Acsany <68116180+acsany@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Bartosz Zaczyński <bartosz.zaczynski@gmail.com>
1 parent a5b9bb9 commit ec9c5ab

6 files changed

Lines changed: 106 additions & 0 deletions

File tree

claude-api-python/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Use the Claude API in Python
2+
3+
This folder contains supporting materials for the Real Python tutorial [How to Use the Claude API in Python](https://realpython.com/claude-api-python/).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import anthropic
2+
3+
client = anthropic.Anthropic()
4+
5+
response = client.messages.create(
6+
model="claude-sonnet-4-6",
7+
max_tokens=1024,
8+
messages=[{"role": "user", "content": "What is the Zen of Python?"}],
9+
)
10+
11+
print(response.content[0].text)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import anthropic
2+
3+
client = anthropic.Anthropic()
4+
5+
system_prompt = """
6+
You are a Python coding assistant. You only answer questions about Python.
7+
If the user asks about any other programming language or unrelated topic,
8+
politely explain that you can only help with Python questions.
9+
"""
10+
11+
user_input = input("Ask me anything about Python: ")
12+
13+
response = client.messages.create(
14+
model="claude-sonnet-4-6",
15+
max_tokens=1024,
16+
system=system_prompt,
17+
messages=[{"role": "user", "content": user_input}],
18+
)
19+
20+
print(f"\n{response.content[0].text}")

claude-api-python/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
anthropic
2+
pydantic
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import anthropic
2+
import json
3+
4+
client = anthropic.Anthropic()
5+
6+
response = client.messages.create(
7+
model="claude-sonnet-4-6",
8+
max_tokens=1024,
9+
system="You are a Python coding assistant.",
10+
messages=[
11+
{
12+
"role": "user",
13+
"content": "Write a Python function that adds two numbers.",
14+
}
15+
],
16+
output_config={
17+
"format": {
18+
"type": "json_schema",
19+
"schema": {
20+
"type": "object",
21+
"properties": {
22+
"function_name": {"type": "string"},
23+
"code": {"type": "string"},
24+
"explanation": {"type": "string"},
25+
},
26+
"required": ["function_name", "code", "explanation"],
27+
"additionalProperties": False,
28+
},
29+
}
30+
},
31+
)
32+
33+
result = json.loads(response.content[0].text)
34+
35+
print("--- Approach 1: Handwritten JSON schema ---")
36+
print(f"Function: {result['function_name']}")
37+
print(f"\nCode:\n{result['code']}")
38+
print(f"\nExplanation: {result['explanation']}")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import anthropic
2+
3+
from pydantic import BaseModel
4+
5+
6+
class FunctionDescription(BaseModel):
7+
function_name: str
8+
code: str
9+
explanation: str
10+
11+
12+
client = anthropic.Anthropic()
13+
14+
response = client.messages.parse(
15+
model="claude-sonnet-4-6",
16+
max_tokens=1024,
17+
system="You are a Python coding assistant.",
18+
messages=[
19+
{
20+
"role": "user",
21+
"content": "Write a Python function that adds two numbers.",
22+
}
23+
],
24+
output_format=FunctionDescription,
25+
)
26+
27+
result = response.parsed_output
28+
29+
print("--- Approach 2: Pydantic + client.messages.parse() ---")
30+
print(f"Function: {result.function_name}")
31+
print(f"\nCode:\n{result.code}")
32+
print(f"\nExplanation: {result.explanation}")

0 commit comments

Comments
 (0)