Structured outputs

When using the .create method in the Python SDK, we can generate structured outputs if we provide a pydantic model with our prompt:

from notdiamond import NotDiamond
from pydantic import BaseModel, Field

class LanguageChoice(BaseModel):
    language: str = Field(description="The programming language of choice.")
    reason: str = Field(description="The reason to pick the programming language.")

messages = [
        {"role": "system", "content": "You are a world class software developer."},
        {"role": "user", "content": "What language would you suggest for developing a web application?"}
    ]

llm_providers = ['openai/gpt-3.5-turbo', 'openai/gpt-4-turbo-2024-04-09', 
                 'openai/gpt-4o-2024-05-13', 'anthropic/claude-3-opus-20240229']

client = NotDiamond()

result, session_id, _ = client.chat.completions.create(
        messages=messages,
        model=llm_providers,
        response_model=LanguageChoice
    )

print(result)

We can also do this with a streamed response. Response model fields will start with None and gradually build out as the response is streamed. Example:

from notdiamond import NotDiamond
from pydantic import BaseModel, Field

class LanguageChoice(BaseModel):
    language: str = Field(description="The programming language of choice.")
    reason: str = Field(description="The reason to pick the programming language.")

messages = [
        {"role": "system", "content": "You are a world class software developer."},
        {"role": "user", "content": "What language would you suggest for developing a web application?"}
    ]

llm_providers = ['openai/gpt-3.5-turbo', 'openai/gpt-4-turbo-2024-04-09', 
                 'openai/gpt-4o-2024-05-13', 'anthropic/claude-3-opus-20240229']

client = NotDiamond(llm_configs=llm_providers)

result = client.chat.completions.stream(
        messages=messages,
        response_model=LanguageChoice
    )
for chunk in result:
    print(chunk)

📘

Not all models support structured outputs

Visit the supported LLM models page to see which models support structured outputs.