Files
tfm_ainventory/venv/lib/python3.12/site-packages/anthropic/lib/foundry.md
Daniel Bedeleanu ea49cd6e4a feat(phase1): add image storage utilities
- Create backend/services/image_storage.py with 4 core functions:
  - sanitize_filename(): remove unsafe chars, limit to 255 chars, convert to lowercase
  - get_unique_filename(): handle collisions with UUID suffix (format: {name}_{uuid8}_{variant}.jpg)
  - ensure_image_directories(): create /images/ root and category subdirs on startup
  - save_image(): save bytes to /images/{category}/{filename}, returns relative path
- Create comprehensive test suite (22 tests) covering all functionality
- Integrate ensure_image_directories() into FastAPI startup event
- Directory structure: /images/{category}/{filename}
- Collision handling: auto-suffix with UUID if filename exists
- All tests passing, pathlib.Path for safe operations
2026-04-20 21:57:26 +03:00

2.7 KiB

Anthropic Foundry

To use this library with Foundry, use the AnthropicFoundry class instead of the Anthropic class.

Installation

pip install anthropic

Usage

Basic Usage with API Key

from anthropic import AnthropicFoundry

client = AnthropicFoundry(
    api_key="...",  # defaults to ANTHROPIC_FOUNDRY_API_KEY environment variable
    resource="my-resource",  # your Foundry resource
)

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)

print(message.content[0].text)

Using Azure AD Token Provider

For enhanced security, you can use Azure AD (Microsoft Entra) authentication instead of an API key:

from anthropic import AnthropicFoundry
from azure.identity import DefaultAzureCredential
from azure.identity import get_bearer_token_provider

credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(
    credential, 
    "https://ai.azure.com/.default"
)

client = AnthropicFoundry(
    azure_ad_token_provider=token_provider,
    resource="my-resource",
)

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)

print(message.content[0].text)

Examples

Streaming Messages

from anthropic import AnthropicFoundry

client = AnthropicFoundry(
    api_key="...",
    resource="my-resource",
)

with client.messages.stream(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a haiku about programming"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Async Usage

from anthropic import AsyncAnthropicFoundry

async def main():
    client = AsyncAnthropicFoundry(
        api_key="...",
        resource="my-resource",
    )
    
    message = await client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello!"}],
    )
    
    print(message.content[0].text)

import asyncio
asyncio.run(main())

Async Streaming

from anthropic import AsyncAnthropicFoundry

async def main():
    client = AsyncAnthropicFoundry(
        api_key="...",
        resource="my-resource",
    )
    
    async with client.messages.stream(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Write a haiku about programming"}],
    ) as stream:
        async for text in stream.text_stream:
            print(text, end="", flush=True)

import asyncio
asyncio.run(main())