fix: Nvidia - fix structured output syntax#3058
Conversation
anakin87
left a comment
There was a problem hiding this comment.
If I run the test locally with your changes, the integration test using NVIDIA_API_KEY still fails. In this PR, it does not run for security reasons.
Are you sure that it works for you? Please investigate more...
|
This seems to work instead import json
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.nvidia import NvidiaChatGenerator
json_schema = {
"type": "object",
"properties": {"title": {"type": "string"}, "rating": {"type": "number"}},
"required": ["title", "rating"],
}
prompt = (
"Return the title and the rating based on the following movie review.\n"
"Review: Inception is a really well made film. I rate it four stars out of five."
)
component = NvidiaChatGenerator(
model="meta/llama-3.1-70b-instruct",
generation_kwargs={
"response_format": {
"type": "json_schema",
"json_schema": {"name": "movie_review", "schema": json_schema},
},
},
)
results = component.run([ChatMessage.from_user(prompt)])
text = results["replies"][0].text
print(repr(text))
output = json.loads(text)
print(f"Parsed: title={output['title']}, rating={output['rating']}")Feel free to experiment more and maybe we can steer this PR to just show this new syntax for NVIDIA. Let me know... |
|
Thanks for checking this! Will push an updated version shortly. |
|
I have updated the implementation to follow the newer NVIDIA API syntax.This change is now applied consistently in both Please let me know if this aligns better with the expected approach or if further adjustments are needed! |
|
Thanks for the feedback! |
anakin87
left a comment
There was a problem hiding this comment.
Thank you!
I applied some minor changes
|
Thanks a lot for the review and for applying the final changes! Really appreciate the guidance on aligning with the newer NVIDIA response_format syntax. |
Related Issues
Proposed Changes
The
guided_jsonschema passed viageneration_kwargs["extra_body"]was incorrectly nested when sent to the NVIDIA API.This resulted in payloads like:
{ "extra_body": { "extra_body": { "guided_json": {...} } } }Because of this, the model ignored the schema and returned non-JSON output, causing
JSONDecodeErrorin tests.Fix
extra_bodybefore making API callsrunandrun_asyncsuper()to avoid issues with the@componentdecoratorThis ensures
guided_jsonis correctly forwarded to the NVIDIA API.How did you test it?
Result:
Notes for the reviewer
Checklist
fix:)