Magentic

Latest version: v0.32.0

Safety actively analyzes 682416 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 2 of 10

0.32.0

What's Changed

Add support for OpenAI "strict" setting for structured outputs. This guarantees that the generated JSON schema matches that supplied by the user. In magentic, this is set via an extension of pydantic's `ConfigDict`, and works for pydantic models as well as functions. See the docs for more info https://magentic.dev/structured-outputs/#configdict

For a BaseModel

python
from magentic import prompt, ConfigDict
from pydantic import BaseModel


class Superhero(BaseModel):
model_config = ConfigDict(openai_strict=True)

name: str
age: int
power: str
enemies: list[str]


prompt("Create a Superhero named {name}.")
def create_superhero(name: str) -> Superhero: ...


create_superhero("Garden Man")


For a function

python
from typing import Annotated, Literal

from magentic import ConfigDict, with_config
from pydantic import Field


with_config(ConfigDict(openai_strict=True))
def activate_oven(
temperature: Annotated[int, Field(description="Temp in Fahrenheit", lt=500)],
mode: Literal["broil", "bake", "roast"],
) -> str:
"""Turn the oven on with the provided settings."""
return f"Preheating to {temperature} F with mode {mode}"


prompt(
"Do some cooking",
functions=[
activate_oven,
...



PRs

* Add support for OpenAI structured outputs by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/305

**Full Changelog**: https://github.com/jackmpcollins/magentic/compare/v0.31.0...v0.32.0

0.31.0

What's Changed
* Add Anthropic vision by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/304
- See https://magentic.dev/vision/


**Full Changelog**: https://github.com/jackmpcollins/magentic/compare/v0.30.0...v0.31.0

0.30.0

What's Changed

> [!WARNING]
> Breaking change: `StructuredOutputError` has been replaced by more specific exceptions `StringNotAllowedError` and `ToolSchemaParseError` in PR https://github.com/jackmpcollins/magentic/pull/288

🤖 ♻️ LLM-Assisted retries has been added. When enabled, this sends incorrectly formatted output back to the LLM along with the error message to have the LLM fix its mistakes. This can be used to enforce more complex validation on output schemas using pydantic validators.

For example, placing an arbitrary constraint on a string field

python
from typing import Annotated

from magentic import prompt
from pydantic import AfterValidator, BaseModel


def assert_is_ireland(v: str) -> str:
if v != "Ireland":
raise ValueError("Country must be Ireland")
return v


class Country(BaseModel):
name: Annotated[str, AfterValidator(assert_is_ireland)]
capital: str


prompt(
"Return a country",
max_retries=3,
)
def get_country() -> Country: ...


get_country()

0.29.0

What's Changed
* Make Message a pydantic model / serializable by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/294

This means `Message` objects can be used anywhere pydantic models can, including in prompt-functions. The new `AnyMessage` type simplifies this. For example
python
from magentic import AnyMessage, prompt

prompt("Create an example of few-shot prompting for a chatbot")
def make_few_shot_prompt() -> list[AnyMessage]: ...

make_few_shot_prompt()
[SystemMessage('You are a helpful and knowledgeable assistant.'),
UserMessage('What’s the weather like today?'),
AssistantMessage[Any]('The weather today is sunny with a high of 75°F (24°C).'),
UserMessage('Can you explain the theory of relativity in simple terms?'),
AssistantMessage[Any]('Sure! The theory of relativity, developed by Albert Einstein, ...]


Dependabot
* Bump logfire-api from 0.46.1 to 0.49.0 by dependabot in https://github.com/jackmpcollins/magentic/pull/292
* Bump logfire from 0.46.1 to 0.49.0 by dependabot in https://github.com/jackmpcollins/magentic/pull/293
* Bump pytest from 8.2.2 to 8.3.2 by dependabot in https://github.com/jackmpcollins/magentic/pull/286
* Bump openai from 1.35.13 to 1.38.0 by dependabot in https://github.com/jackmpcollins/magentic/pull/290
* Bump mypy from 1.10.1 to 1.11.1 by dependabot in https://github.com/jackmpcollins/magentic/pull/291


**Full Changelog**: https://github.com/jackmpcollins/magentic/compare/v0.28.1...v0.29.0

0.28.1

What's Changed
* Bump ruff from 0.4.10 to 0.5.2 by dependabot in https://github.com/jackmpcollins/magentic/pull/269
* Limit tool call id to 9 chars for Mistral by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/279


**Full Changelog**: https://github.com/jackmpcollins/magentic/compare/v0.28.0...v0.28.1

0.28.0

What's Changed

🪵 🔥 Logfire / OpenTelemetry now supported!

This makes it much easier to follow what tool calls are being made by the LLM both as printed output locally and in Logfire or another monitoring service. It also lets you see the raw requests being sent to OpenAI/Anthropic so you can more easily debug issues.

All it takes to get set up is

sh
pip install logfire

python
import logfire

logfire.configure(send_to_logfire=False) Or True to use the Logfire service
logfire.instrument_openai() optional, to trace OpenAI API calls
logfire.instrument_anthropic() optional, to trace Anthropic API calls


Check out the new docs page: https://magentic.dev/logging-and-tracing/

PRs

* ~Add basic logging and MAGENTIC_VERBOSE env var by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/263~
* Update dependencies by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/264
* Instrument for Logfire / OpenTelemetry by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/265
* Do not set stream_options when using AzureOpenAI by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/262
* Use new `parallel_tool_calls` arg with OpenAI API by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/267
* Fix LitellmChatModel tool_choice parameter to force Anthropic tool use by jackmpcollins in https://github.com/jackmpcollins/magentic/pull/268


**Full Changelog**: https://github.com/jackmpcollins/magentic/compare/v0.27.0...v0.28.0

Page 2 of 10

© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.