fix(structured outputs): send structured output beta header when format is omitted#1158
Conversation
…ut_format` is missing (anthropics#1121)" This reverts commit 062077e.
|
That change made the normal Now, when a user calls Simple repro script: # /// script
# requires-python = ">=3.12"
# dependencies = [
# "anthropic==0.77.0",
# "rich",
# ]
# ///
import json
from typing_extensions import Literal
import rich
from anthropic import Anthropic, beta_tool
client = Anthropic()
@beta_tool
def get_weather(location: str, units: Literal["c", "f"]) -> str:
"""Lookup the weather for a given city in either celsius or fahrenheit
Args:
location: The city and state, e.g. San Francisco, CA
units: Unit for the output, either 'c' for celsius or 'f' for fahrenheit
Returns:
A dictionary containing the location, temperature, and weather condition.
"""
# Simulate a weather API call
print(f"Fetching weather for {location} in {units}")
# Here you would typically make an API call to a weather service
# For demonstration, we return a mock response
if units == "c":
return json.dumps(
{
"location": location,
"temperature": "20°C",
"condition": "Sunny",
}
)
else:
return json.dumps(
{
"location": location,
"temperature": "68°F",
"condition": "Sunny",
}
)
def main() -> None:
runner = client.beta.messages.tool_runner(
max_tokens=1024,
model="claude-sonnet-4-5",
tools=[get_weather],
messages=[{"role": "user", "content": "What is the weather in SF?"}],
)
for message in runner:
rich.print(message)
main() |
|
is it feasible to do the more correct thing and not send an empty string? or is that much more involved |
|
I’m also preparing a more accurate fix in the codegen, though I’m not sure what we should do:
Just changing it could probably break some code, so the second option seems safer |
|
I don’t want to just change the codegen behavior to stop sending an empty header value in this case, because empty values are valid in HTTP and might be useful in some cases |
|
fwiw I think it'd be pretty safe to just not send an empty header by default, that seems like more sane behaviour to me. |
|
sounds like this is a bad break though so we can just merge this for now |
|
can you also add a test for this case as well so we can't regress in the future? |
This reverts commit 062077e.