From bcf7bfdf5526d794995721fe7d6bb4269326dd53 Mon Sep 17 00:00:00 2001 From: Nuno Bispo Date: Thu, 26 Mar 2026 09:06:20 +0100 Subject: [PATCH 1/4] added materials for How to Use the Claude API in Python for AI-Powered Applications --- .../basic_claude_call.py | 13 +++++++ .../coding_assistant.py | 22 +++++++++++ .../requirements.txt | 2 + .../structured_output_1.py | 38 +++++++++++++++++++ .../structured_output_2.py | 31 +++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 how-to-use-claude-api-in-python/basic_claude_call.py create mode 100644 how-to-use-claude-api-in-python/coding_assistant.py create mode 100644 how-to-use-claude-api-in-python/requirements.txt create mode 100644 how-to-use-claude-api-in-python/structured_output_1.py create mode 100644 how-to-use-claude-api-in-python/structured_output_2.py diff --git a/how-to-use-claude-api-in-python/basic_claude_call.py b/how-to-use-claude-api-in-python/basic_claude_call.py new file mode 100644 index 0000000000..41ef584a83 --- /dev/null +++ b/how-to-use-claude-api-in-python/basic_claude_call.py @@ -0,0 +1,13 @@ +import anthropic + +client = anthropic.Anthropic() + +response = client.messages.create( + model="claude-sonnet-4-5", + max_tokens=1024, + messages=[ + {"role": "user", "content": "What is the Zen of Python?"} + ], +) + +print(response.content[0].text) diff --git a/how-to-use-claude-api-in-python/coding_assistant.py b/how-to-use-claude-api-in-python/coding_assistant.py new file mode 100644 index 0000000000..4baf89f445 --- /dev/null +++ b/how-to-use-claude-api-in-python/coding_assistant.py @@ -0,0 +1,22 @@ +import anthropic + +client = anthropic.Anthropic() + +system_prompt = """ +You are a Python coding assistant. You only answer questions about Python. +If the user asks about any other programming language or unrelated topic, +politely explain that you can only help with Python questions. +""" + +user_input = input("Ask me anything about Python: ") + +response = client.messages.create( + model="claude-sonnet-4-5", + max_tokens=1024, + system=system_prompt, + messages=[ + {"role": "user", "content": user_input} + ], +) + +print(f"\n{response.content[0].text}") diff --git a/how-to-use-claude-api-in-python/requirements.txt b/how-to-use-claude-api-in-python/requirements.txt new file mode 100644 index 0000000000..8ba2115811 --- /dev/null +++ b/how-to-use-claude-api-in-python/requirements.txt @@ -0,0 +1,2 @@ +anthropic +pydantic \ No newline at end of file diff --git a/how-to-use-claude-api-in-python/structured_output_1.py b/how-to-use-claude-api-in-python/structured_output_1.py new file mode 100644 index 0000000000..17c7671cd7 --- /dev/null +++ b/how-to-use-claude-api-in-python/structured_output_1.py @@ -0,0 +1,38 @@ +import anthropic +import json + +client = anthropic.Anthropic() + +response = client.messages.create( + model="claude-sonnet-4-5", + max_tokens=1024, + system="You are a Python coding assistant.", + messages=[ + { + "role": "user", + "content": "Write a Python function that adds two numbers.", + } + ], + output_config={ + "format": { + "type": "json_schema", + "schema": { + "type": "object", + "properties": { + "function_name": {"type": "string"}, + "code": {"type": "string"}, + "explanation": {"type": "string"}, + }, + "required": ["function_name", "code", "explanation"], + "additionalProperties": False, + }, + } + }, +) + +result = json.loads(response.content[0].text) + +print("--- Approach 1: Hand-written JSON schema ---") +print(f"Function: {result['function_name']}") +print(f"\nCode:\n{result['code']}") +print(f"\nExplanation: {result['explanation']}") \ No newline at end of file diff --git a/how-to-use-claude-api-in-python/structured_output_2.py b/how-to-use-claude-api-in-python/structured_output_2.py new file mode 100644 index 0000000000..f72ae9fc54 --- /dev/null +++ b/how-to-use-claude-api-in-python/structured_output_2.py @@ -0,0 +1,31 @@ +import anthropic + +from pydantic import BaseModel + + +class FunctionDescription(BaseModel): + function_name: str + code: str + explanation: str + +client = anthropic.Anthropic() + +response = client.messages.parse( + model="claude-sonnet-4-5", + max_tokens=1024, + system="You are a Python coding assistant.", + messages=[ + { + "role": "user", + "content": "Write a Python function that adds two numbers.", + } + ], + output_format=FunctionDescription, +) + +result = response.parsed_output + +print("--- Approach 2: Pydantic + client.messages.parse() ---") +print(f"Function: {result.function_name}") +print(f"\nCode:\n{result.code}") +print(f"\nExplanation: {result.explanation}") From 3990d5c073e1852c407d98108ba4f99219e2e55a Mon Sep 17 00:00:00 2001 From: Nuno Bispo Date: Thu, 2 Apr 2026 10:00:07 +0200 Subject: [PATCH 2/4] Updated code after first TR --- how-to-use-claude-api-in-python/basic_claude_call.py | 2 +- how-to-use-claude-api-in-python/coding_assistant.py | 2 +- how-to-use-claude-api-in-python/structured_output_1.py | 2 +- how-to-use-claude-api-in-python/structured_output_2.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/how-to-use-claude-api-in-python/basic_claude_call.py b/how-to-use-claude-api-in-python/basic_claude_call.py index 41ef584a83..2a7776a533 100644 --- a/how-to-use-claude-api-in-python/basic_claude_call.py +++ b/how-to-use-claude-api-in-python/basic_claude_call.py @@ -3,7 +3,7 @@ client = anthropic.Anthropic() response = client.messages.create( - model="claude-sonnet-4-5", + model="claude-sonnet-4-6", max_tokens=1024, messages=[ {"role": "user", "content": "What is the Zen of Python?"} diff --git a/how-to-use-claude-api-in-python/coding_assistant.py b/how-to-use-claude-api-in-python/coding_assistant.py index 4baf89f445..776e24bddf 100644 --- a/how-to-use-claude-api-in-python/coding_assistant.py +++ b/how-to-use-claude-api-in-python/coding_assistant.py @@ -11,7 +11,7 @@ user_input = input("Ask me anything about Python: ") response = client.messages.create( - model="claude-sonnet-4-5", + model="claude-sonnet-4-6", max_tokens=1024, system=system_prompt, messages=[ diff --git a/how-to-use-claude-api-in-python/structured_output_1.py b/how-to-use-claude-api-in-python/structured_output_1.py index 17c7671cd7..8734f96b1e 100644 --- a/how-to-use-claude-api-in-python/structured_output_1.py +++ b/how-to-use-claude-api-in-python/structured_output_1.py @@ -4,7 +4,7 @@ client = anthropic.Anthropic() response = client.messages.create( - model="claude-sonnet-4-5", + model="claude-sonnet-4-6", max_tokens=1024, system="You are a Python coding assistant.", messages=[ diff --git a/how-to-use-claude-api-in-python/structured_output_2.py b/how-to-use-claude-api-in-python/structured_output_2.py index f72ae9fc54..742a32500a 100644 --- a/how-to-use-claude-api-in-python/structured_output_2.py +++ b/how-to-use-claude-api-in-python/structured_output_2.py @@ -11,7 +11,7 @@ class FunctionDescription(BaseModel): client = anthropic.Anthropic() response = client.messages.parse( - model="claude-sonnet-4-5", + model="claude-sonnet-4-6", max_tokens=1024, system="You are a Python coding assistant.", messages=[ From 0408af453c2ba77f17f75c80c00fb8b6422d3fae Mon Sep 17 00:00:00 2001 From: Philipp Acsany <68116180+acsany@users.noreply.github.com> Date: Fri, 17 Apr 2026 22:42:25 +0200 Subject: [PATCH 3/4] Apply ruff format to fix Linux314 CI Pure formatter changes; no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) --- how-to-use-claude-api-in-python/basic_claude_call.py | 4 +--- how-to-use-claude-api-in-python/coding_assistant.py | 4 +--- how-to-use-claude-api-in-python/structured_output_1.py | 2 +- how-to-use-claude-api-in-python/structured_output_2.py | 1 + 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/how-to-use-claude-api-in-python/basic_claude_call.py b/how-to-use-claude-api-in-python/basic_claude_call.py index 2a7776a533..59a3f3f417 100644 --- a/how-to-use-claude-api-in-python/basic_claude_call.py +++ b/how-to-use-claude-api-in-python/basic_claude_call.py @@ -5,9 +5,7 @@ response = client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, - messages=[ - {"role": "user", "content": "What is the Zen of Python?"} - ], + messages=[{"role": "user", "content": "What is the Zen of Python?"}], ) print(response.content[0].text) diff --git a/how-to-use-claude-api-in-python/coding_assistant.py b/how-to-use-claude-api-in-python/coding_assistant.py index 776e24bddf..84990445c6 100644 --- a/how-to-use-claude-api-in-python/coding_assistant.py +++ b/how-to-use-claude-api-in-python/coding_assistant.py @@ -14,9 +14,7 @@ model="claude-sonnet-4-6", max_tokens=1024, system=system_prompt, - messages=[ - {"role": "user", "content": user_input} - ], + messages=[{"role": "user", "content": user_input}], ) print(f"\n{response.content[0].text}") diff --git a/how-to-use-claude-api-in-python/structured_output_1.py b/how-to-use-claude-api-in-python/structured_output_1.py index 8734f96b1e..59e3c7b583 100644 --- a/how-to-use-claude-api-in-python/structured_output_1.py +++ b/how-to-use-claude-api-in-python/structured_output_1.py @@ -35,4 +35,4 @@ print("--- Approach 1: Hand-written JSON schema ---") print(f"Function: {result['function_name']}") print(f"\nCode:\n{result['code']}") -print(f"\nExplanation: {result['explanation']}") \ No newline at end of file +print(f"\nExplanation: {result['explanation']}") diff --git a/how-to-use-claude-api-in-python/structured_output_2.py b/how-to-use-claude-api-in-python/structured_output_2.py index 742a32500a..af392ddd62 100644 --- a/how-to-use-claude-api-in-python/structured_output_2.py +++ b/how-to-use-claude-api-in-python/structured_output_2.py @@ -8,6 +8,7 @@ class FunctionDescription(BaseModel): code: str explanation: str + client = anthropic.Anthropic() response = client.messages.parse( From dc225c70c2f2e7e4304e1e5d4fd037be00873282 Mon Sep 17 00:00:00 2001 From: Philipp Acsany <68116180+acsany@users.noreply.github.com> Date: Fri, 17 Apr 2026 22:46:19 +0200 Subject: [PATCH 4/4] Add README.md Co-Authored-By: Claude Opus 4.7 (1M context) --- how-to-use-claude-api-in-python/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 how-to-use-claude-api-in-python/README.md diff --git a/how-to-use-claude-api-in-python/README.md b/how-to-use-claude-api-in-python/README.md new file mode 100644 index 0000000000..508fe054d0 --- /dev/null +++ b/how-to-use-claude-api-in-python/README.md @@ -0,0 +1,3 @@ +# How to Use the Claude API in Python + +This folder contains supporting materials for the Real Python tutorial [How to Use the Claude API in Python](https://realpython.com/claude-api-python/).