Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ async def aset_input_attributes(span, kwargs):
span, f"{prefix}.input_schema", json.dumps(input_schema)
)

output_format = kwargs.get("output_format")
if output_format and isinstance(output_format, dict):
if output_format.get("type") == "json_schema" and output_format.get("schema"):
set_span_attribute(
span,
SpanAttributes.LLM_REQUEST_STRUCTURED_OUTPUT_SCHEMA,
json.dumps(output_format.get("schema")),
)


async def _aset_span_completions(span, response):
if not should_send_prompts():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ def set_model_request_attributes(span, kwargs, llm_model):
span, SpanAttributes.LLM_FREQUENCY_PENALTY, kwargs.get("frequency_penalty")
)

generation_config = kwargs.get("generation_config")
if generation_config and hasattr(generation_config, "response_schema"):
try:
_set_span_attribute(
span,
SpanAttributes.LLM_REQUEST_STRUCTURED_OUTPUT_SCHEMA,
json.dumps(generation_config.response_schema),
)
except Exception:
pass

if "response_schema" in kwargs:
try:
_set_span_attribute(
span,
SpanAttributes.LLM_REQUEST_STRUCTURED_OUTPUT_SCHEMA,
json.dumps(kwargs.get("response_schema")),
)
except Exception:
pass


@dont_throw
def set_response_attributes(span, response, llm_model):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from anthropic import Anthropic
from traceloop.sdk import Traceloop
from dotenv import load_dotenv

load_dotenv()

client = Anthropic()

Traceloop.init(
app_name="anthropic_structured_outputs_demo",
)


def main():
print("Making request with structured outputs...")

joke_schema = {
"type": "object",
"properties": {
"joke": {
"type": "string",
"description": "A joke about OpenTelemetry"
},
"rating": {
"type": "integer",
"description": "Rating of the joke from 1 to 10"
}
},
"required": ["joke", "rating"],
"additionalProperties": False
}

response = client.beta.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
betas=["structured-outputs-2025-11-13"],
messages=[
{
"role": "user",
"content": "Tell me a joke about OpenTelemetry and rate it from 1 to 10"
}
],
output_format={
"type": "json_schema",
"schema": joke_schema
}
)

print("\n=== Response ===")
print(response.content[0].text)
print("\n=== The 'gen_ai.request.structured_output_schema' attribute should be logged ===")


if __name__ == "__main__":
main()
49 changes: 49 additions & 0 deletions packages/sample-app/sample_app/gemini_structured_outputs_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import google.generativeai as genai
from traceloop.sdk import Traceloop
from dotenv import load_dotenv

load_dotenv()

genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))

Traceloop.init(
app_name="gemini_structured_outputs_demo",
)


def main():
print("Making request with structured outputs...")

response_schema = {
"type": "object",
"properties": {
"joke": {
"type": "string",
"description": "A joke about OpenTelemetry"
},
"rating": {
"type": "integer",
"description": "Rating of the joke from 1 to 10"
}
},
"required": ["joke", "rating"]
}

model = genai.GenerativeModel("gemini-1.5-flash")

result = model.generate_content(
"Tell me a joke about OpenTelemetry and rate it",
generation_config=genai.GenerationConfig(
response_mime_type="application/json",
response_schema=response_schema
)
)

print("\n=== Response ===")
print(result.text)
print("\n=== The 'gen_ai.request.structured_output_schema' attribute should be logged ===")


if __name__ == "__main__":
main()
38 changes: 38 additions & 0 deletions packages/sample-app/sample_app/openai_structured_outputs_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
from openai import OpenAI
from pydantic import BaseModel
from traceloop.sdk import Traceloop
from dotenv import load_dotenv

Comment thread
coderabbitai[bot] marked this conversation as resolved.
load_dotenv()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

Traceloop.init(
app_name="structured_outputs_demo",
)


class Joke(BaseModel):
joke: str
rating: int


def main():
print("Making request with structured outputs...")
response = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[{"role": "user", "content": "Tell me a joke about OpenTelemetry"}],
response_format=Joke,
)

print("\n=== Response ===")
print(f"Joke: {response.choices[0].message.parsed.joke}")
print(f"Rating: {response.choices[0].message.parsed.rating}")
print(
"\n=== Check the span output above for 'gen_ai.request.structured_output_schema' attribute ==="
)


if __name__ == "__main__":
main()
Loading