-
Notifications
You must be signed in to change notification settings - Fork 0
fix: extract LangChain content-block text and apply model parameters after eval #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewklatzke
wants to merge
1
commit into
main
Choose a base branch
from
aklatzke/AIC-3351/fix-content-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """ | ||
| Example: langchain_messages() against a Claude model with extended thinking turned on. | ||
|
|
||
| With thinking on, Anthropic returns ``content`` as a list of blocks — a ``thinking`` block followed | ||
| by a ``text`` block — instead of a plain string. A handler that only reads string content reports | ||
| an empty response for these runs while the tokens are still spent, so this example fails loudly | ||
| when no text comes back. | ||
|
|
||
| Anthropic omits thinking from the turn that follows a tool result, so give this a prompt the model | ||
| can answer on its own — a run that goes through the tool loop ends on a plain string and never | ||
| reaches the block-shaped content this exercises. | ||
|
|
||
| Usage (via main.py): | ||
| python main.py langchain-thinking <flag-key> "Reason it out yourself without any tools: what is 17 times 23?" | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| from examples.tools import ( | ||
| fetch_launchdarkly_documentation, | ||
| get_preferences, | ||
| search_ld_documentation, | ||
| ) | ||
| from examples.utils import new_context, write_output | ||
| from launchdarkly_ai_langchain_messages import create_langchain_messages_handler | ||
| from launchdarkly_ai_server import config | ||
|
|
||
| # Anthropic requires max_tokens to exceed the thinking budget. | ||
| _THINKING_BUDGET_TOKENS = 1024 | ||
| _MAX_TOKENS = 4096 | ||
|
|
||
|
|
||
| async def run(key: str, user_input: str) -> None: | ||
| from langchain_anthropic import ChatAnthropic | ||
|
|
||
| def build_model(ai_config: Any) -> Any: | ||
| model = ai_config.get("model") or {} | ||
| raw = model.get("parameters") | ||
| parameters: dict[str, Any] = dict(raw) if isinstance(raw, dict) else {} | ||
| return ChatAnthropic( | ||
| timeout=None, | ||
| stop=None, | ||
| **parameters, | ||
| model_name=str(model.get("name") or "claude-sonnet-4-5"), | ||
| thinking={"type": "enabled", "budget_tokens": _THINKING_BUDGET_TOKENS}, | ||
| max_tokens_to_sample=_MAX_TOKENS, | ||
| ) | ||
|
|
||
| response = await config( | ||
| key=key, | ||
| handler=create_langchain_messages_handler(llm=build_model), | ||
| tool_handlers={ | ||
| "get-user-preferences": get_preferences, | ||
| "search-ld-documentation": search_ld_documentation, | ||
| "fetch-launchdarkly-documentation": fetch_launchdarkly_documentation, | ||
| }, | ||
| ).invoke(user_input, new_context(), variables={"user_input": user_input}) | ||
|
|
||
| if not (response.response or "").strip(): | ||
| raise RuntimeError( | ||
| "Model returned no text. A thinking-enabled model returns content as a list of " | ||
| "blocks, and the handler dropped it." | ||
| ) | ||
|
|
||
| print(json.dumps(response, indent=2, default=str)) | ||
| write_output(response) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example factory kwargs collide with parameters
Medium Severity
The thinking example spreads flag
model.parametersintoChatAnthropicand then passes overlapping keywords (timeout,stop,model_name,thinking,max_tokens_to_sample). A second value for any of those keys is aTypeError, andmax_tokensaliasesmax_tokens_to_sample, so a typical Claude flag never constructs the model.Reviewed by Cursor Bugbot for commit 7beaa6f. Configure here.